Kotlin Reference
|
This section documents Kotlin 2.4.x on the JVM, as published at kotlinlang.org, which is the reference these pages are written and verified against. This content was generated with the assistance of AI and should be verified against kotlinlang.org before being relied on in production. This section’s bibliography lists the reference material consulted while preparing these pages. |
Welcome to the Kotlin reference. Kotlin is a modern, statically-typed language for the JVM (and, via Kotlin Multiplatform, JS/Native/Wasm) created by JetBrains — concise syntax, null safety built into the type system, and full Java interoperability. This section documents Kotlin 2.4.x on the JVM, written and verified against kotlinlang.org.
If you are new to Kotlin, read Getting Started and Kotlin and the JVM first, then the Language fundamentals pages in order, followed by Object-oriented and functional programming — from there the remaining groups (the type system, collections and metaprogramming, error handling, coroutines, and Kotlin on Android and tooling) can be read in any order as you need them. For Kotlin’s server-side use, see Java or Kotlin for Spring Boot?.
What’s covered
Getting started
-
Getting Started — what Kotlin is, installing it,
kotlinc, the Kotlin REPL, the Kotlin Playground, and "Hello, World". -
Kotlin and the JVM — Kotlin/JVM bytecode, Java interoperability, where Kotlin/JVM runs, and Kotlin Multiplatform’s other targets.
Language fundamentals
-
Lexical Structure and Style — file/package structure, identifiers, comments, optional semicolons, literals, and the official coding conventions.
-
Basic Types and Variables —
val/var, type inference, the built-in number/Boolean/Char types,Any/Unit/Nothing, arrays, and unsigned integers. -
Strings and Text — string templates, raw/triple-quoted strings, and common
Stringoperations. -
Operators and Ranges — arithmetic/comparison/logical operators, ranges and progressions, and infix notation.
-
Control Flow —
if/whenas expressions,for/while, and labeledbreak/continue. -
Functions — default and named arguments, single-expression functions,
vararg, local functions, andtailrec.
Object-oriented and functional programming
-
Classes and Objects — primary/secondary constructors, properties,
initblocks, visibility modifiers, and "classes are final by default." -
Inheritance and Interfaces —
open/override, abstract classes, interfaces with default/property members, and class delegation viaby. -
Data Classes and Destructuring —
data class, generated members,copy(),componentN(), and destructuring declarations. -
Sealed Classes and Enums —
sealed class/interface, exhaustivewhen, andenum class. -
Objects and Companion Objects — singletons, companion objects, and object expressions.
-
Extension Functions and Scope Functions — extension functions/properties, and
let/run/with/apply/also. -
Lambdas and Higher-Order Functions — lambda syntax, closures, function types, and
inline/noinline/crossinline.
Type system
-
Generics and Variance — declaration-site variance (
in/out), star projections, andreifiedtype parameters. -
Null Safety — nullable types,
?.,?:,!!,as?, platform types, and smart casts. -
Equality and Operator Overloading — structural vs. referential equality, and the overloadable operator conventions.
Collections and metaprogramming
-
Collections and Sequences — read-only vs. mutable collections, functional operations, and
Sequence(lazy) vs. eager collections. -
Annotations and Reflection — declaring annotations, meta-annotations, and basic
kotlin.reflect. -
Type-Safe Builders and DSLs — lambdas with receiver and building a small type-safe builder.
Error handling
-
Exceptions and Error Handling —
tryas an expression, no checked exceptions, custom exceptions, andResult/runCatching.
Coroutines
-
Coroutines Basics —
suspendfunctions, coroutine builders,CoroutineScope, and structured concurrency. -
Coroutine Context, Cancellation and Exceptions —
Dispatchers, cancellation and timeouts,SupervisorJob, andCoroutineExceptionHandler. -
Flows — cold
Flowbasics, operators,StateFlowvs.SharedFlow, and buffering.
Kotlin on Android & tooling
-
Kotlin for Android — Android KTX, Jetpack pointers, and coroutines on Android.
-
Build and Tooling — the Gradle Kotlin DSL, Maven’s Kotlin plugin, IDE support, and static analysis.
-
Testing —
kotlin.test, JUnit 5 with Kotlin, MockK, andkotlinx-coroutines-test.
Reference
-
Cheat Sheet (PDF) — a single-page, printable summary of everything in this section, with a downloadable PDF.
Bibliography
-
kotlinlang.org — the official Kotlin documentation home, and the primary source for every page in this section.
-
the Kotlin standard library API reference — the reference every stdlib type and function in this section links to conceptually.
-
the Kotlin language specification — the authoritative reference for the language-level pages.
-
the Kotlin coroutines guide — the primary source for the Coroutines group.
-
the Android Kotlin developer guide — the primary source for Kotlin for Android.
-
Aigner, Sebastian, Roman Elizarov, Svetlana Isakova, and Dmitry Jemerov. Kotlin in Action, 2nd ed. Manning Publications, 2024. ISBN 9781617299599. Consulted as part of the bibliography for this section, cross-checked against its public table of contents only (the full text was not reachable while preparing these pages) — see the publisher’s book page and manning.com.
The book above is a consulted reference, not the primary source for this section; on any discrepancy, or wherever it is silent on a feature introduced after it was written, the official Kotlin documentation wins.