Skip to content

Instantly share code, notes, and snippets.

@audreyfeldroy
Last active May 23, 2024 03:12
Show Gist options
  • Save audreyfeldroy/d058c1b86e18b7dbaa9e4a1907e7b10b to your computer and use it in GitHub Desktop.
Save audreyfeldroy/d058c1b86e18b7dbaa9e4a1907e7b10b to your computer and use it in GitHub Desktop.
Audrey's Kotlin for Developers Cheatsheet

Variables

Declare var for a mutable variable:

var name = "John"
name = "Jane"

Declare val for a read-only constant:

val pi = 3.14

Functions

fun greeting(): String {
    return "Hello!"
}

println(greeting())

Classes

class Person(var name: String)

val person = Person("John")
person.name = "Jane"

Collections

val numbers = mutableListOf(1,2,3)
numbers.add(4)

val numsSet = setOf(1,2,3)
println(numsSet.contains(2))

Control Flow

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