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.

Languages·47 commands·Last updated 2026-07-21
kotlingradlejvm

Compile & Run 5

kotlinc main.kt -include-runtime -d main.jar
Compile to an executable jar
java -jar main.jar
Run the compiled jar
kotlinc -script script.kts
Run a Kotlin script
kotlinc-jvm
Enter the REPL
kotlin main.kt
Run directly (no compile)

Gradle Build 5

./gradlew build
Build the project
./gradlew run
Run the main class
./gradlew test
Run tests
./gradlew clean
Clean the build dir
./gradlew dependencies
Show dependencies

Null Safety 5

var name: String? = null
Nullable type declaration
name?.length
Safe call (null -> null)
name ?: "default"
Elvis operator (default on null)
name!!.length
Non-null assertion (throws on null)
val len = name?.length ?: 0
Combine the two

Collections 10

list.filter { it > 5 }
Filter
list.map { it * 2 }
Map
list.flatMap { it.split(",") }
FlatMap
list.groupBy { it.category }
GroupBy
list.sortedBy { it.age }
SortedBy
list.distinct()
Distinct
list.take(5)
Take first 5
list.drop(3)
Drop first 3
list.firstOrNull { it.id == 1 }
Find first match
list.partition { it > 0 }
Partition by predicate

Coroutines 6

runBlocking { ... }
Blocking coroutine (tests)
launch { ... }
Launch (no return value)
async { ... }
Async (returns Deferred)
delay(1000)
Non-blocking delay
withContext(Dispatchers.IO) { ... }
Switch context
coroutineScope { ... }
Create a coroutine scope

Classes & Objects 8

data class User(val name: String, val age: Int)
Data class (auto equals/hashCode/toString/copy)
sealed class Result
Sealed class (exhaustive when)
object Singleton { val x = 1 }
Singleton object
companion object { val MAX = 100 }
Companion object (class-level)
class User(val name: String)
Primary ctor declares property
open class Animal; class Dog : Animal()
Inheritance (parent must be open)
interface Drawable { fun draw() }
Interface definition
enum class Color { RED, GREEN, BLUE }
Enum class

Scope Functions & Delegation 8

obj.let { it.foo() }
let: it receiver, returns lambda result
obj.apply { this.x = 1 }
apply: this receiver, returns the object (init)
obj.also { it.log() }
also: it receiver, returns object (side-effects)
obj.run { compute() }
run: this receiver, returns lambda result
with(obj) { foo() }
with: this receiver, returns lambda result
val lazyVal by lazy { init() }
Lazy init delegation (computed on first access)
var x by Delegates.observable(0) { _, old, new -> }
Observable property delegation
val v by map
Map delegation (keyed access)

Tips

  • 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