Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

This cheat sheet covers the essential Kotlin syntax students need when reading, writing, and debugging modern Kotlin programs. It is useful as a quick classroom reference because Kotlin uses concise patterns that can be easy to forget at first. Students can use it to compare variable declarations, functions, control flow, classes, and Android starter code without searching through full documentation.

The most important ideas are type inference, null safety, object-oriented structure, collection operations, and coroutine basics. Kotlin uses val for read-only variables, var for mutable variables, fun for functions, and class or data class for defining types. Null safety relies on ?, ?:, and ?. to prevent common runtime crashes.

Android Kotlin code often uses lifecycle methods, view setup, listeners, and ViewModel patterns to organize app behavior.

Key Facts

  • Use val name = "Ada" for a read-only variable and var score = 0 for a variable whose value can change.
  • Declare a function with fun add(a: Int, b: Int): Int { return a + b } or use an expression body with fun add(a: Int, b: Int) = a + b.
  • Use if as an expression, such as val result = if (score >= 60) "pass" else "retry".
  • Use a nullable type with String? and safely access it with name?.length or provide a default with name ?: "Unknown".
  • Create a data class with data class User(val id: Int, val name: String) to automatically get useful functions like toString and copy.
  • Loop through a range with for (i in 1..5) and loop through a collection with for (item in items).
  • Use listOf("a", "b") for a read-only list and mutableListOf("a", "b") when elements must be added or removed.
  • Launch coroutine work from a scope with scope.launch { } and call suspend functions only from another suspend function or coroutine.

Vocabulary

val
A Kotlin keyword for declaring a read-only variable that cannot be reassigned after it receives a value.
var
A Kotlin keyword for declaring a mutable variable that can be reassigned later in the program.
Nullable type
A type marked with ? that allows a variable to hold either a normal value or null.
Data class
A class mainly used to store data and automatically provide functions such as equals, toString, and copy.
Coroutine
A Kotlin feature for running asynchronous or long-running work without blocking the main thread.
Lambda
An anonymous function written as a value, often used with collection operations and event listeners.

Common Mistakes to Avoid

  • Using var when val is enough is a mistake because it allows reassignment that the program may not need, making bugs easier to introduce.
  • Forgetting the ? on a value that may be null is a mistake because Kotlin will not let a non-null type store null, and unsafe workarounds can cause crashes.
  • Calling a suspend function from normal code is a mistake because suspend functions must run inside another suspend function or a coroutine scope.
  • Confusing == and === is a mistake because == checks value equality while === checks whether two references point to the exact same object.
  • Updating Android UI work from a background thread is a mistake because UI changes must happen on the main thread to avoid errors and unpredictable behavior.

Practice Questions

  1. 1 Write Kotlin code that declares a read-only variable age with the value 17 and a mutable variable points with the value 0, then increases points by 10.
  2. 2 Given val numbers = listOf(2, 4, 6, 8), what is the result of numbers.map { it * 3 }?
  3. 3 Write a Kotlin function named isPassing that takes score: Int and returns true when score is at least 60.
  4. 4 Explain why Kotlin requires safe calls like user?.name when user has the type User?, and how this helps prevent program crashes.