Kotlin is a modern, statically-typed programming language developed by JetBrains that runs on the Java Virtual Machine (JVM), compiles to native binaries, and WebAssembly, and is officially supported for Android development. Since Google announced Kotlin as a first-class language for Android in 2017, it has become the preferred choice for millions of developers due to its concise syntax, null safety guarantees, and seamless Java interoperability. The K2 compiler β stable since Kotlin 2.0 β brings significantly faster compilation and unified behaviour across all platforms. Understanding Kotlin's type system and its convention-based approach (where language features like operator overloading and property delegation are implemented through specific naming conventions) is key to writing idiomatic, expressive code across Android, server-side, and Kotlin Multiplatform projects.
What This Cheat Sheet Covers
This topic spans 22 focused tables and 167 indexed concepts, 156 flashcards. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: Null Safety Features
Kotlin's headline feature: the type system tracks whether a value can be null, turning a whole class of runtime crashes into compile errors. The operators here are how you work with that distinction β declaring nullable types with ?, navigating them safely with ?. and the Elvis ?:, and only resorting to the !! assertion when you're truly certain a value is present.
| Feature | Example | Description | |
|---|---|---|---|
var name: String? = null | β’ Append ? to any type to allow null valuesβ’ compiler enforces explicit handling before access. | ||
val length = name?.length | β’ Returns null if the receiver is nullβ’ chains safely without throwing NullPointerException. | ||
val len = name?.length ?: 0 | Provides default value when the left-hand expression evaluates to null. | ||
if (x != null) x.length | Compiler automatically casts to non-nullable type after a null check β no explicit cast needed. | ||
name?.let { print(it.length) } | β’ Executes lambda only if receiver is non-null β’ useful for null-safe transformations. | ||
listOf(1, null, 2).filterNotNull() | β’ Returns a list with all null elements removedβ’ result type is List<T> (non-nullable). | ||
val x: String? = obj as? String | Returns null instead of throwing ClassCastException when the cast fails. | ||
val len = name!!.length | β’ Throws NullPointerException if nullβ’ use sparingly and only when absolutely certain value is non-null. |