Kotlin
Kotlin is a modern, statically typed language that runs on the JVM and interoperates seamlessly with Java — you can call Java from Kotlin and vice versa, and adopt it gradually in an existing codebase. It's the default, Google-recommended language for Android, and is increasingly common for backend services and multiplatform code.
Its headline features are null safety (nullability is part of the type system, eliminating a huge class of NullPointerExceptions) and coroutines (lightweight, readable async), wrapped in concise syntax that strips away Java's boilerplate.
TL;DR
- A modern, concise JVM language that's fully interoperable with Java.
- Null safety is built into the type system (
StringvsString?). - Coroutines make async/concurrent code readable.
- The default for Android; also strong for JVM backends and multiplatform.
Quick Example
Null safety and a data class in action — the compiler forces you to handle the nullable case:
Core Concepts
- Null safety —
Stringcan't be null;String?can, and the compiler enforces handling (?.,?:,!!). data class— concise value types withequals/hashCode/copygenerated.- Sealed classes — model a closed set of states/types (great with
when). - Extension functions — add methods to existing types without subclassing.
- Coroutines —
suspendfunctions and structured concurrency for async without callback hell.
Common Uses
- Android apps — the primary, recommended language.
- Backend services — Spring (Kotlin support), Ktor, Micronaut.
- Kotlin Multiplatform — share business logic across Android, iOS, web, and backend.
- Scripting/tooling — Gradle build scripts (Kotlin DSL).
Best Practices
- Embrace null safety — avoid
!!; model absence with nullable types or sealed classes. - Use coroutines with structured concurrency; don't block threads inside them.
- Prefer immutability (
val, data classes) and small, focused functions. - Keep Gradle builds reproducible (pinned versions, toolchain).
Comparison: Kotlin vs Java
See Java.
Common Mistakes
Blocking inside a coroutine
Overusing scope functions
FAQ
Kotlin or Java?
Both run on the JVM and interoperate freely. Kotlin is more concise, has null safety built in, and is the Android default — increasingly favored for new JVM code. Java has the larger talent pool and ecosystem maturity. Many teams write new code in Kotlin while maintaining Java services.
How does Kotlin's null safety work?
Types are non-nullable by default (String); to allow null you write String?, and the compiler then forces you to handle the null case with safe calls (?.), the Elvis operator (?:), or explicit assertions (!!). This eliminates most NullPointerExceptions at compile time.
What are coroutines?
Kotlin's mechanism for asynchronous, non-blocking code that reads sequentially. suspend functions can pause without blocking a thread, and structured concurrency ties their lifetimes to a scope, making cancellation and error handling tractable — far more readable than callbacks.
Can I use Kotlin with existing Java code?
Yes — interop is seamless in both directions, so you can add Kotlin file-by-file to a Java project, call Java libraries directly, and migrate gradually. Watch nullability when calling Java APIs that lack annotations.
Related Topics
- Java — The interoperable JVM language
- Mobile Development — Kotlin on Android
- Kotlin for Android — Android specifics
- Programming Languages — The broader landscape