Last active
May 23, 2024 03:12
-
-
Save audreyfeldroy/d058c1b86e18b7dbaa9e4a1907e7b10b to your computer and use it in GitHub Desktop.
Audrey's Kotlin for Developers Cheatsheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Variables | |
Declare var for a mutable variable: | |
```kotlin | |
var name = "John" | |
name = "Jane" | |
``` | |
Declare val for a read-only constant: | |
```kotlin | |
val pi = 3.14 | |
``` | |
## Functions | |
```kotlin | |
fun greeting(): String { | |
return "Hello!" | |
} | |
println(greeting()) | |
``` | |
## Classes | |
```kotlin | |
class Person(var name: String) | |
val person = Person("John") | |
person.name = "Jane" | |
``` | |
## Collections | |
```kotlin | |
val numbers = mutableListOf(1,2,3) | |
numbers.add(4) | |
val numsSet = setOf(1,2,3) | |
println(numsSet.contains(2)) | |
``` | |
## Control Flow | |
```kotlin | |
fun max(a: Int, b: Int): Int { | |
return if (a > b) a else b | |
} | |
val result = max(1, 2) | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment