# Kotlin Language Reference
## Languages — The Codex Coding
**URL:** https://thecodex.expert/coding/languages/kotlin/
**Type:** language-reference | **Confidence:** high | **Last verified:** 2026-06-01

## CANONICAL DEFINITION
Kotlin is a statically typed, multi-platform programming language developed by JetBrains that compiles to JVM bytecode, JavaScript, and native code — offering null safety, concise syntax, 100% Java interoperability, and coroutines for asynchronous programming, and is Google's preferred language for Android development.

## QUICK FACTS
- **Released:** February 2016 (1.0); created by JetBrains
- **Current:** Kotlin 2.x (2024)
- **Targets:** JVM bytecode, JavaScript (Kotlin/JS), Native/LLVM (Kotlin/Native)
- **Typing:** Static, null-safe
- **Java interop:** 100% bidirectional — Kotlin calls Java, Java calls Kotlin
- **Android:** Google preferred language since 2019 (first-class since 2017)

## CRITICAL NOTES
- **val ≠ immutable object** — val means immutable reference (like Java final); `val list = mutableListOf()` is valid and mutable
- **Coroutines are NOT threads** — suspendable via CPS transformation; millions can run on a few threads
- **Kotlin + Java coexist** — mix freely in same project; no need to rewrite Java code
- **String vs String?** — String is non-null (compiler enforces); String? is nullable; distinct types
- **Sealed class subclasses** must be in same package/compilation unit as sealed class

## NULL SAFETY
- `T` = non-nullable (default); `T?` = nullable
- Safe call: `user?.name` — returns null if user is null
- Elvis: `user?.name ?: "Anonymous"` — fallback if null
- Non-null assertion: `user!!.name` — throws KotlinNullPointerException if null (use sparingly)
- Smart cast: after `if (user != null)`, compiler knows user is non-null
- `let`: `user?.let { u -> println(u.name) }` — runs block only if non-null

## KEY FEATURES
- `data class User(val name: String, val age: Int)` — auto-generates equals/hashCode/toString/copy/component1..N
- Extension functions: `fun String.isPalindrome() = this == reversed()`
- Sealed classes: restrict hierarchy + exhaustive when (compiler-verified)
- `when` expression: returns value; exhaustive for sealed types
- `object` keyword: singleton; companion object (replaces static)
- Destructuring: `val (id, name) = user`
- Default parameters + named arguments: `fun f(x: Int = 0, y: Int = 0)`

## COROUTINES
- `suspend fun` — function that can be suspended (not blocking a thread)
- `launch { }` — fire-and-forget coroutine
- `async { } / await()` — coroutine returning a value
- `delay(ms)` — non-blocking suspend (unlike Thread.sleep)
- Dispatchers: `Dispatchers.Main` (UI), `Dispatchers.IO` (I/O), `Dispatchers.Default` (CPU)
- Structured concurrency: coroutines launched in scope; scope cancelled → children cancelled
- Implementation: CPS transformation by compiler — state machine, not OS threads

## KOTLIN MULTIPLATFORM (KMP)
- Stable since November 2023
- Share business logic across Android, iOS, web, desktop, server
- NOT shared UI (unlike Flutter) — each platform uses native UI
- Compiles to: JVM (Android), LLVM (iOS/macOS/Linux), JS (web)

## SOURCES
- Kotlin spec: kotlinlang.org/spec/
- Kotlin docs: kotlinlang.org/docs/
- Coroutines guide: kotlinlang.org/docs/coroutines-guide.html

*The Codex Coding — thecodex.expert/coding/ — Free forever — Mumbai, India*
