Kotlin Cheat Sheet

Free Kotlin cheat sheet: the most-used Kotlin syntax and methods at a glance — searchable and beginner-friendly.

Basics & Variables

ConceptSyntaxExample
Immutable / mutable variable
val is read-only; var can be reassigned. Prefer val.
val x = ... / var y = ...val name = "Ada" var count = 0
Explicit type
Types are usually inferred but can be annotated.
val x: Type = ...val age: Int = 36
String template
$ inlines a variable; ${} inlines any expression.
"text $var ${expr}"println("Hi $name, age ${age + 1}")
Main entry point
The program starts here.
fun main() { ... }fun main() { println("Hello") }
Print
print() omits the trailing newline.
println(x)println("Result: $count")

Null Safety

ConceptSyntaxExample
Nullable type
The ? makes null an allowed value for that type.
var x: Type?var name: String? = null
Safe call
Returns null instead of throwing if x is null.
x?.memberval len = name?.length
Elvis operator
Provides a fallback value when the left side is null.
x ?: defaultval len = name?.length ?: 0
Not-null assertion
Throws NullPointerException if x is null. Use sparingly.
x!!val len = name!!.length
Safe cast
Returns null instead of throwing on a bad cast.
x as? Typeval s = obj as? String

Functions & Lambdas

ConceptSyntaxExample
Function
Parameter types are required; return type follows the params.
fun name(p: T): R { ... }fun add(a: Int, b: Int): Int { return a + b }
Expression body
Single-expression functions skip braces and return.
fun f(...) = exprfun square(n: Int) = n * n
Default & named args
Call with greet(name = "Ada") to name arguments.
fun f(p: T = default)fun greet(name: String = "World") = "Hi $name"
Lambda
An anonymous function value; the type can be inferred.
{ x -> body }val double = { n: Int -> n * 2 }
Higher-order function
it is the implicit name of a single lambda parameter.
fun f(op: (T) -> R)list.map { it * 2 }

Collections

ConceptSyntaxExample
List
listOf is read-only; mutableListOf allows add/remove.
listOf(...) / mutableListOf(...)val nums = listOf(1, 2, 3)
Map
to builds a key-value Pair.
mapOf(k to v)val ages = mapOf("Ada" to 36, "Bob" to 28)
Set
Stores only distinct values.
setOf(...)val unique = setOf(1, 1, 2) // {1, 2}
Transform
Functional operations return new collections.
.map / .filternums.filter { it > 1 }.map { it * 10 }
Iterate
forEach { } is the lambda-based alternative.
for (x in coll)for (n in nums) println(n)

Classes & Objects

ConceptSyntaxExample
Class with constructor
Primary constructor params with val/var become properties.
class C(val p: T)class Person(val name: String, var age: Int)
Data class
Auto-generates equals, hashCode, toString and copy.
data class C(...)data class Point(val x: Int, val y: Int)
Inheritance
Classes are final by default; mark the parent open.
class B : A()open class Animal class Dog : Animal()
Interface
Implement with class C : Shape.
interface I { fun f() }interface Shape { fun area(): Double }
Singleton object
A single, lazily-created instance.
object Name { ... }object Config { val version = "1.0" }

Coroutines

ConceptSyntaxExample
Suspend function
Can pause and resume without blocking a thread.
suspend fun f()suspend fun fetch(): String { delay(1000) return "done" }
Launch a coroutine
Fire-and-forget; returns a Job you can cancel.
launch { ... }scope.launch { val data = fetch() }
Async / await
async returns a Deferred result you await later.
async { } then .await()val d = async { fetch() } val result = d.await()
Coroutine scope
Waits for all child coroutines before returning.
coroutineScope { ... }coroutineScope { launch { task() } }
Non-blocking delay
Suspends the coroutine without blocking the thread.
delay(ms)delay(500)