Language cheatsheet
Kotlin 2 Cheatsheet
Kotlin syntax and idioms: null safety, data classes, collections, extension and scope functions, sealed types and coroutines.
Tooling
- Check the compiler version — Print the Kotlin compiler version.
- Compile and run a file — Compile to a jar and run it with the Kotlin runtime included.
- Run a script — Run a Kotlin script file (.kts).
- Build and test with Gradle — Build, run the tests and run the application in a Gradle project.
Basics and null safety
- Declare variables — val is read-only and var is mutable. Types are inferred.
- Insert values in strings — Use $name or ${expression} inside a string.
- Handle nulls — A type with ? can be null. Use ?. to call safely, ?: for a default, and !! only when you are certain.
- Branch with when — when is a flexible switch that can be an expression.
- Define functions — Parameters can have defaults and be passed by name. Single-expression functions drop the braces.
- Check a type and use it — After an is check, Kotlin smart-casts the value to that type.
- Validate arguments and state — require checks arguments and check verifies state, both throwing on failure.
Classes and types
- Data classes — A data class gets equals, hashCode, toString, copy and destructuring for free.
- Classes and constructors — The primary constructor is part of the class header. Properties can have getters.
- Sealed types for fixed alternatives — A sealed interface limits its implementations, so when must handle every case.
- Singletons and companion objects — object declares a singleton, and a companion object holds class-level members.
- Add a function to an existing type — An extension function is called like a member but defined outside the class.
- Interfaces and delegation — Implement an interface by delegating to another object with by.
- Compute a value on first use — lazy runs its block once, the first time the property is read.
Collections and lambdas
- Create collections — listOf, setOf and mapOf are read-only. Use mutableListOf and friends to change them.
- Transform and filter — Chain operations on collections with lambdas. it names a single parameter.
- Group, sort and count — Group items by a key, sort them and count matches.
- Process lazily — Sequences compute items on demand, which avoids intermediate lists for large pipelines.
- Use scope functions — let transforms an object, apply configures it, also adds a side effect, and run/with compute a result.
- Loop over a map — Destructure entries and ranges in for loops.
Coroutines
- Add coroutines — Coroutine support is a separate library. Add it to your Gradle dependencies.
- Start coroutines — runBlocking bridges regular code to coroutines. launch starts concurrent work and returns a Job.
- Run work in parallel — async starts work that returns a value, and await gets it. Both calls below run at the same time.
- Switch dispatcher — Run blocking or CPU-heavy work on a suitable dispatcher without blocking the caller.
- Stream values with Flow — A flow emits values over time and is collected from a coroutine.
- Limit how long work can take — Cancel a block that takes too long.
Related cheatsheets
- C++ 23 cheatsheet — Compile and debug C++ with g++ and CMake, and use modern C++ (up to C++23): containers, algorithms, smart pointers, templates and threads.
- C# 14 cheatsheet — Modern C# 14 syntax for strings, nullability, collections, LINQ, records, pattern matching, async code and generics.
- CSS 3 cheatsheet — Layout with flexbox and grid, responsive design, custom properties, animations and modern selectors.
- Go 1 cheatsheet — The go command for modules, building, testing and cross-compiling, plus the code patterns you write every day.
- HTML 5 cheatsheet — HTML5 page structure, text, links, images, forms, tables and metadata, with accessible patterns.
- Java, Maven and Gradle 25 cheatsheet — JDK commands to compile, run and diagnose Java programs, plus the Maven and Gradle commands for builds and tests.