Kotlin is a modern, statically‑typed language from JetBrains, running on the JVM and beyond (JavaScript, Native, WebAssembly). It emphasises conciseness, safety (null‑safety by design), and full interoperability with Java. Latest stable version (May 2025) is 2.0.0, featuring the new K2 compiler that unifies all back‑ends and dramatically reduces build time (up to 94 % faster on large projects) .
Note: If you know Java, you already understand ~90 % of Kotlin’s runtime ecosystem.
val
immutability).
// Immutable
val greeting: String = "Hello"
// Mutable
var counter = 0 // inferred Int
for (i in 1..3) { println(i) }
when (score) {
10 -> println("Perfect")
in 0..4 -> println("Try again")
else -> println("Nice")
}
Kotlin models absence explicitly with ?
types and safe‑call ?.
:
val maybeName: String? = fetchName()
println(maybeName?.length ?: 0)
fun add(a: Int, b: Int): Int = a + b
numbers.filter { it % 2 == 0 }
.map { it * it }
Note: Inline functions eliminate call‑site overhead – critical for HOF‑heavy code (e.g. DSLs).
data class User(val id: Int, val name: String)
sealed interface Result<T>
data class Success<T>(val data: T): Result<T>
data class Failure(val ex: Throwable): Result<Nothing>
Note: The compiler exhaustively checks when
over sealed
types – no else
needed.
val names = listOf("Ada","Grace")
println(names.joinToString())
class Box<out T>(val value:T)
Use in
/ out
for consumer‑producer variance instead of Java’s wildcards.
scope.launch {
val weather = async { api.weather() }
val news = async { api.headlines() }
show(weather.await(), news.await())
}
Cold asynchronous streams with operators (~Rx). Back‑pressure handled via suspending emissions.
Share business logic (commonMain
) across Android, iOS, Desktop, Web, and Embedded targets. JetBrains’ 2025 roadmap focuses on
Compose Multiplatform for seamless shared UIs and first‑party Swift‑export tooling .
cinterop
tool.
plugins{
kotlin("jvm") version "2.0.0"
}
dependencies{
implementation(platform(libs.kotlin.bom))
}
context (Transaction, Logger)
fun Account.transfer(amount:Money, to:Account){ ... }
@JvmInline
value class Email(val value:String)
Note: Adopt -Xexplicit-api=strict
for library development to catch accidental public leaks.
Use kotlin.test
, JUnit5
, MockK
, and kotest
for expressive DSLs.
val
> var
.requireNotNull
.sealed
instead of enum when carrying data.The K2 compiler removes a major bottleneck for incremental builds; migrate with the official guide .
Note: All examples compile under Kotlin 2.0 on the JVM with -Xcontext-receivers
enabled.