Kotlin

On this page 7

Kotlin is a statically typed, general-purpose programming language developed by JetBrains that compiles to JVM bytecode, JavaScript, WebAssembly and native machine code. Among modern programming languages, it is best known as the preferred language for Android development and as a concise, safer alternative to Java that remains fully interoperable with existing Java code.

Kotlin combines object-oriented and functional programming, adds null safety to the type system and offers coroutines for asynchronous code. The project became open source in 2012, reached version 1.0 in 2016 and moved to the K2 compiler with Kotlin 2.0 in 2024.

How Kotlin compiles and runs

Kotlin source code is processed by a single compiler front end and then handed to one of several back ends, each targeting a different platform. The most widely used back end emits Java bytecode, so Kotlin programs run on any Java Virtual Machine and can call Java libraries directly, while Java code can call Kotlin classes in return.

The other back ends make Kotlin a multiplatform language. Kotlin/Native uses LLVM to produce standalone binaries for iOS, macOS, Linux and Windows, while Kotlin/JS and Kotlin/Wasm target browsers and JavaScript runtimes. The following table summarizes the compilation targets.

TargetOutputTypical use
Kotlin/JVMJava bytecodeAndroid apps, backend services
Kotlin/NativeNative binaries via LLVMiOS apps, command-line tools
Kotlin/JSJavaScript codeWeb front ends, Node.js
Kotlin/WasmWebAssembly modulesHigh-performance browser code
Kotlin compilation targets

Kotlin Multiplatform builds on these targets by letting a project share business logic, networking and data layers across platforms, while each platform keeps its own native user interface. The expect and actual declarations define where shared code needs a platform-specific implementation.

Key language features

Kotlin was designed to remove common sources of boilerplate and runtime failures found in older JVM languages. Its features target safety, conciseness and readability rather than novelty, and most of them have no runtime cost.

FeatureWhat it doesProblem it addresses
Null safetySeparates nullable String? from non-null StringNull pointer exceptions
Data classesGenerate equals, hashCode and copy automaticallyRepetitive value-object code
Type inferenceDerives types from assigned valuesVerbose declarations
Extension functionsAdd methods to existing classesUtility classes and wrappers
Sealed classesRestrict a hierarchy to known subtypesIncomplete conditional handling
CoroutinesWrite asynchronous code sequentiallyCallback nesting and thread overhead
Selected Kotlin features and their purpose

Null safety is the most influential of these features. The compiler refuses to dereference a nullable value until the code proves it is not null, using safe calls (?.), the Elvis operator (?:) or smart casts after an explicit check. The non-null assertion operator !! bypasses this protection and should be rare in well-written code.

Coroutines provide lightweight concurrency built around suspending functions. Thousands of coroutines can run on a small thread pool, and structured concurrency ties each coroutine to a scope, so cancellation and error handling propagate predictably instead of leaking background work.

Kotlin compared with Java and other languages

Kotlin is most often compared with Java, because both share the same runtime and ecosystem, and with Swift, because the two languages fill similar roles on mobile platforms. The table below highlights the practical differences.

AspectKotlinJavaSwiftScala
Null handlingIn the type systemAnnotations and OptionalIn the type systemOption type
Primary platformsJVM, Android, multiplatformJVMApple platformsJVM
Asynchronous modelCoroutinesVirtual threadsasync and awaitFutures and libraries
Learning curveModerateModerateModerateSteep
Java interoperabilitySeamlessNativeNoneGood
Kotlin compared with Java, Swift and Scala

Recent Java releases have narrowed the gap with records, pattern matching and virtual threads, so the choice between the two increasingly depends on team skills and platform rather than raw capability. Compared with Scala, Kotlin deliberately offers a smaller, more pragmatic feature set, which shortens onboarding and keeps compile times more predictable.

Where Kotlin is used

Kotlin is used wherever the JVM is used, and increasingly in cross-platform mobile projects. Google declared Kotlin the preferred language for Android in 2019, and modern Android libraries, including the declarative Jetpack Compose user interface toolkit, are designed Kotlin-first.

AreaTypical toolsWhy teams choose Kotlin
Android appsJetpack Compose, Android StudioOfficial support and modern libraries
Backend servicesSpring Boot, KtorJava ecosystem with less boilerplate
Shared mobile logicKotlin MultiplatformOne codebase for Android and iOS logic
Build scriptsGradle Kotlin DSLType-checked, IDE-assisted configuration
Main Kotlin application areas

For backend work, Spring Boot supports Kotlin directly, and Ktor offers a lightweight, coroutine-based framework from JetBrains. Teams migrating from Java usually adopt Kotlin file by file, because both languages compile into the same project without wrappers.

Limitations and common mistakes

Kotlin has trade-offs that experienced teams plan for. Clean builds can be slower than equivalent Java builds, although the K2 compiler and incremental compilation reduce the difference considerably. Kotlin/Native and Multiplatform tooling are younger than the JVM toolchain, so some libraries remain JVM-only.

  • Overusing `!!`: forcing non-null values reintroduces the null pointer exceptions the type system was meant to prevent.
  • Platform types from Java: values returned by unannotated Java code have unknown nullability, so the compiler cannot protect them fully.
  • Blocking inside coroutines: calling blocking input or output on the main dispatcher freezes the user interface; use Dispatchers.IO instead.
  • GlobalScope launches: starting coroutines outside a structured scope causes leaks and lost exceptions.
  • Excessive cleverness: chaining many scope functions such as let, apply and also can make code harder to read than plain statements.

The safest practice is to annotate Java APIs for nullability, keep coroutines inside lifecycle-aware scopes and agree on a team style guide for idiomatic but readable Kotlin.

In everyday language

Kotlin is a language programmers use to write apps, especially apps for Android phones. It was created to do the same jobs as Java, an older and very popular language, but with shorter and safer code.

Think of a modern kitchen fitted into an old house. The plumbing and wiring stay the same, so everything still works. The new tools simply make cooking faster and safer. Kotlin catches many mistakes before the app ever runs, which means fewer crashes for the people who use it.

Seen in the real world

A mid-sized bank maintains an Android banking app written in Java, with about 300,000 lines of code. Crash reports show that a large share of failures are null pointer exceptions, and the team also needs an iOS version that behaves identically when calculating fees and validating transfers.

The team adopts Kotlin gradually: new screens are written in Kotlin with Jetpack Compose, while existing Java classes keep working unchanged. Validation rules, fee calculations and API clients move into a Kotlin Multiplatform module that both the Android and iOS apps use, with each app keeping its own native interface.

Within a year, null-related crashes drop sharply because new code must handle missing values explicitly, data classes remove thousands of lines of boilerplate and business rules exist in one place, so an interest calculation fix ships to both platforms in a single change.