Skip to content

Introduction to Kotlin

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; NullPointerException is 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.
TargetRuntimeOutputUse Case
JVMJava Virtual Machine.class bytecodeServer, Android, existing Java
Kotlin/NativeLLVMNative binariesiOS, macOS, Linux, embedded
Kotlin/JSJavaScript engines.js / IRFrontend, 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 actual
actual fun platformName(): String = "JVM ${System.getProperty("java.version")}"
// Native actual
actual fun platformName(): String = "Kotlin/Native"
  • 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.
Terminal window
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install kotlin
Terminal window
brew install kotlin
Terminal window
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)

The Kotlin Playground provides an in-browser editor with JVM, JavaScript, and Kotlin/Native targets. Useful for quick experimentation without a local setup.

fun main() {
println("Hello, World!")
}

Compile and run:

Terminal window
kotlinc hello.kt -include-runtime -d hello.jar
java -jar hello.jar

Or run directly:

Terminal window
kotlin hello.kt

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.

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>

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.

build.gradle.kts
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).
  • ** 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 kotlinc directly for production builds. Always use Gradle or Maven for dependency management, incremental compilation, and reproducible builds.

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.