Kotlin Cheatsheet - Kotlin Syntax & Coroutine Reference
This reference is for Kotlin developers — on Android, a JVM backend, or experimenting with scripts — and covers the five things you touch constantly: compiling and running with kotlinc/Gradle, null-safety idioms (safe-call, elvis, the non-null assertion), collection operators like filter/map/groupBy, and coroutines for structured concurrency. Unlike a generic Kotlin grammar dump, entries are grouped by the problem each snippet solves, from "avoid the NPE" to "run I/O off the main thread". After reading, you should be able to write null-safe chains without !! except where intended, transform collections fluently, and launch coroutines scoped so cancellation propagates correctly.
Compile & Run 5
kotlinc main.kt -include-runtime -d main.jarjava -jar main.jarkotlinc -script script.ktskotlinc-jvmkotlin main.ktGradle Build 5
./gradlew build./gradlew run./gradlew test./gradlew clean./gradlew dependenciesNull Safety 5
var name: String? = nullname?.lengthname ?: "default"name!!.lengthval len = name?.length ?: 0Collections 10
list.filter { it > 5 }list.map { it * 2 }list.flatMap { it.split(",") }list.groupBy { it.category }list.sortedBy { it.age }list.distinct()list.take(5)list.drop(3)list.firstOrNull { it.id == 1 }list.partition { it > 0 }Coroutines 6
runBlocking { ... }launch { ... }async { ... }delay(1000)withContext(Dispatchers.IO) { ... }coroutineScope { ... }Classes & Objects 8
data class User(val name: String, val age: Int)sealed class Resultobject Singleton { val x = 1 }companion object { val MAX = 100 }class User(val name: String)open class Animal; class Dog : Animal()interface Drawable { fun draw() }enum class Color { RED, GREEN, BLUE }Scope Functions & Delegation 8
obj.let { it.foo() }obj.apply { this.x = 1 }obj.also { it.log() }obj.run { compute() }with(obj) { foo() }val lazyVal by lazy { init() }var x by Delegates.observable(0) { _, old, new -> }val v by mapTips
- Kotlin null safety is a compile-time check - avoid !! when you can.
- Coroutine launch returns a Job; async returns a Deferred you can await.
- Collection chaining is elegant, but watch intermediate-collection memory on large data.
- data class auto-generates copy/equals/hashCode/toString - ideal for data models.
- Scope functions: use apply to init, let/let it to transform, also for side-effects.
- sealed class + when lets the compiler verify exhaustiveness - no else needed.
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us