Kotlin Cheat Sheet
Kotlin syntax, null safety, data classes, coroutines, and extension functions for concise, safe JVM development.
2 PagesIntermediateMar 30, 2026
Basic Syntax
Variables, control flow, and printing.
kotlin
fun main() { val age = 30 // immutable (val) var count = 0 // mutable (var) val name = "Ada" val pi: Double = 3.14159 if (age >= 18) { println("$name is an adult") } for (i in 0 until 5) { println("Count: $i") }}
Null Safety & Data Classes
Compile-time null checks and value objects.
kotlin
data class User(val name: String, val age: Int)fun greet(user: User?) { val name = user?.name ?: "Guest" // safe call + Elvis operator println("Hello, $name")}val user: User? = User("Ada", 30)user?.let { println(it.age) } // scope function, runs if non-nullval u2 = user!! // force-unwrap (throws if null)
Coroutines
Lightweight concurrency with suspend functions.
kotlin
import kotlinx.coroutines.*suspend fun fetchData(): String { delay(1000) // non-blocking suspension return "data"}fun main() = runBlocking { val job = launch { val result = fetchData() println(result) } job.join() val deferred = async { fetchData() } println(deferred.await())}
Core Keywords
Common Kotlin language keywords.
- val/var- read-only and mutable variable declarations
- ?./?:- safe call operator and Elvis (null-coalescing) operator
- data class- auto-generates equals(), hashCode(), toString(), copy()
- sealed class- restricted class hierarchy known at compile time
- when- expressive replacement for switch statements
- extension function- adds a function to an existing type without inheritance
- suspend- marks a function as coroutine-suspendable
- companion object- holds members shared across all instances (like static)
Collection Operations
Functional transformations on lists and maps.
kotlin
val nums = listOf(1, 2, 3, 4, 5)val evens = nums.filter { it % 2 == 0 } // [2, 4]val doubled = nums.map { it * 2 } // [2, 4, 6, 8, 10]val sum = nums.sum() // 15val grouped = nums.groupBy { it % 2 == 0 } // {false=[1,3,5], true=[2,4]}val first = nums.firstOrNull { it > 3 } // 4val byLength = listOf("a", "bb", "cc") .associateBy { it.length } // {1=a, 2=cc}
Scope Functions
let, run, with, apply, and also for concise object handling.
kotlin
val name = person?.let { it.name.uppercase() } // runs only if non-nullval user = User().apply { id = 1 email = "[email protected]"} // returns the receiverval len = "hello".run { length } // returns lambda resultlist.also { println("size: ${it.size}") } // side effect, returns listwith(config) { println(host) println(port)}
Sealed Classes & When
Exhaustive pattern matching over a closed type hierarchy.
kotlin
sealed class Result<out T>data class Success<T>(val data: T) : Result<T>()data class Error(val message: String) : Result<Nothing>()object Loading : Result<Nothing>()fun <T> handle(result: Result<T>): String = when (result) { is Success -> "Got ${result.data}" is Error -> "Failed: ${result.message}" Loading -> "Loading..."} // no else needed - compiler verifies exhaustiveness
Key Language Features
Distinctive Kotlin syntax features.
- val / var- immutable (val) vs mutable (var) bindings
- extension fun- add methods to existing types without inheritance
- data class- auto-generates equals, hashCode, toString, copy
- ?. / ?: / !!- safe call, Elvis default, and non-null assertion
- infix- call functions without dots or parens (a to b)
- companion object- static-like members tied to a class
- lateinit- defer non-null property initialization
- by lazy- compute a value once on first access
Pro Tip
Use `data class` with `copy()` for immutable state updates instead of mutating fields directly — it keeps state predictable in concurrent code.
Was this cheat sheet helpful?
Explore Topics
#Kotlin#KotlinCheatSheet#Programming#Intermediate#BasicSyntax#Null#Safety#Data#OOP#Functions#Concurrency#CheatSheet#SkillVeris