Introduction to Kotlin
Overview
Section titled “Overview”Kotlin is a statically typed language targeting the JVM, Android, JavaScript, and native platforms Via LLVM. Developed by JetBrains, it was designed to address Java”s verbosity, null safety issues, And lack of modern language features while maintaining full interoperability.
Key design goals:
- Null safety at the type level — nullable and non-nullable types are distinct;
NullPointerExceptionis largely eliminated at compile time. - Concise syntax — type inference, data classes, default parameters, extension functions reduce boilerplate significantly versus Java.
- Full Java interoperability — Kotlin compiles to JVM bytecode and can call any Java library without wrappers or adapters.
- Coroutines for async — first-class language support for suspending functions replaces callback hell and reactive boilerplate.
JVM vs Kotlin/Native vs Kotlin/JS
Section titled “JVM vs Kotlin/Native vs Kotlin/JS”| Target | Runtime | Output | Use Case |
|---|---|---|---|
| JVM | Java Virtual Machine | .class bytecode | Server, Android, existing Java |
| Kotlin/Native | LLVM | Native binaries | iOS, macOS, Linux, embedded |
| Kotlin/JS | JavaScript engines | .js / IR | Frontend, Node.js |
All three targets share the same language syntax and standard library. Platform-specific APIs are Accessed through expect/actual declarations.
// Common code (shared across all targets)expect fun platformName(): String
fun greet() = "Hello from ${platformName()}"// JVM actualactual fun platformName(): String = "JVM ${System.getProperty("java.version")}"// Native actualactual fun platformName(): String = "Kotlin/Native"When to Use Each Target
Section titled “When to Use Each Target”- JVM: Default choice. Leverages the mature JVM ecosystem, garbage collector, and existing Java libraries. Android uses the JVM target (with ART/Dalvik as the runtime).
- Kotlin/Native: Required for iOS targets. No garbage collector — uses reference counting with an automated cycle collector. Interop with C/Objective-C/Swift via cinterop.
- Kotlin/JS: Use Kotlin Multiplatform with Compose Multiplatform or when sharing logic between backend and browser. IR compiler produces optimizable JavaScript.
Installation
Section titled “Installation”SDKMAN (recommended)
Section titled “SDKMAN (recommended)”curl -s "https://get.sdkman.io" | bashsource "$HOME/.sdkman/bin/sdkman-init.sh"sdk install kotlinHomebrew (macOS)
Section titled “Homebrew (macOS)”brew install kotlinVerify Installation
Section titled “Verify Installation”kotlin -version## Kotlin version 2.1.0-release (JRE 21.0.2+13)kotlinc -version## info: kotlinc-jvm 2.1.0 (JRE 21.0.2+13)Kotlin Playground
Section titled “Kotlin Playground”The Kotlin Playground provides an in-browser editor with JVM, JavaScript, and Kotlin/Native targets. Useful for quick experimentation without a local setup.
Hello World
Section titled “Hello World”fun main() { println("Hello, World!")}Compile and run:
kotlinc hello.kt -include-runtime -d hello.jarjava -jar hello.jarOr run directly:
kotlin hello.ktKotlin Koans
Section titled “Kotlin Koans”The Kotlin Koans are a series of interactive exercises Covering syntax features from basic to advanced. Each koan is a failing test that you fix by Implementing the correct Kotlin code.
Build Tools
Section titled “Build Tools”Gradle with Kotlin DSL (standard)
Section titled “Gradle with Kotlin DSL (standard)”plugins { kotlin("jvm") version "2.1.0"}
repositories { mavenCentral()}
dependencies { testImplementation(kotlin("test"))}
tasks.test { useJUnitPlatform()}<dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> <version>2.1.0</version></dependency>Language Version and Compatibility
Section titled “Language Version and Compatibility”Kotlin uses a stable backward compatibility policy. Code compiled with Kotlin 1.x will run on Future 1.x releases without recompilation. Major version bumps (rare) may break compatibility.
kotlin { compilerOptions { languageVersion.set(org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_2_0) apiVersion.set(org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_2_0) }}languageVersion: Language features available to the compiler.apiVersion: API surface available at runtime (must be <=languageVersion).
Common Pitfalls
Section titled “Common Pitfalls”- ** Mixing JVM and Native targets in the same module without Kotlin Multiplatform. Use KMP explicitly or keep targets in separate modules.
- *_ Assuming Kotlin/JS has the same runtime characteristics as JVM. No
java._packages, different concurrency model, different performance profile. - ** Using
kotlincdirectly for production builds. Always use Gradle or Maven for dependency management, incremental compilation, and reproducible builds.
Intuition
Section titled “Intuition”Kotlin is the language that modernised the JVM. It runs on the JVM but adds null safety, coroutines, data classes, and extension functions. Null safety is the killer feature: the type system distinguishes between nullable and non-nullable types, eliminating NullPointerException at compile time. Coroutines provide asynchronous programming without callback hell. Kotlin is fully interoperable with Java: you can call Java code from Kotlin and vice versa. It is the preferred language for Android development and is gaining traction on the server side.