Learn Kotlin – tutorial, cheatsheet & exercises
Kotlin is a modern, statically typed language from JetBrains. It runs on the JVM (and also compiles to JavaScript and native code), is 100 % compatible with Java, and is Google's recommended language for Android. The code is considerably more compact and safer than classic Java.
Kotlin's hallmarks: null safety built into the type system (no unexpected NullPointerException), data class, extension functions, expressive collections, when, and coroutines for simple concurrency. Kotlin is used for Android apps, backends (Spring Boot, Ktor), and cross-platform projects (Kotlin Multiplatform).
Hello World in Kotlin
fun main() {
println("Hello World!")
}No class wrapper, no semicolons: main can live directly at the top level.
How to get started with Kotlin
The easiest way to get started without installing anything is the Kotlin Playground at play.kotlinlang.org – your code runs right in the browser. For real projects, use IntelliJ IDEA (the Community Edition is free and Kotlin is built in) or Android Studio for apps: choose "New Project" → Kotlin.
For the command line, install the compiler, e.g. with brew install kotlin (Mac), with SDKMAN (sdk install kotlin), or with winget/Scoop on Windows. You also need a JDK (version 17 or 21). Check the installation:
kotlinc -version
java --versionCompile and run: the compiler produces a JAR with the Kotlin runtime built in, which you start with java -jar. Small scripts (.kts) can also be run directly:
kotlinc hallo.kt -include-runtime -d hallo.jar
java -jar hallo.jar
kotlinc -script skript.ktsFor projects with dependencies (e.g. coroutines), use Gradle with the Kotlin DSL (build.gradle.kts). IntelliJ sets this up automatically when you create a project. Coroutines are a separate library (kotlinx-coroutines-core) that you add as a dependency.
hallo.kt. You do not need a class or a project structure to get started: functions can live directly at the top level.Kotlin learning path
Hello World & main
Your first Kotlin program, output, and comments.
Variables, Types & String Templates
val and var, type inference, number types, and text with values.
Conditions: if and when
if as an expression and the powerful when.
Loops & Ranges
for over ranges and collections, while, and breaking out with labels.
Functions
Default values, named arguments, vararg, and returning multiple values.
Lists, Sets & Maps
The basic Kotlin collection types – read-only or mutable.
Null Safety
Nullable types, safe call, Elvis, smart cast, and !!.
Classes, Properties & Interfaces
Primary constructor, properties, inheritance with open, and interfaces.
Data Classes, Enums & Objects
Data classes with copy, enum classes, singletons, and companion objects.
Extension Functions, Lambdas & Scope Functions
Extending existing types and using functions as values.
Collection Operations & Sequences
filter, map, groupBy, fold, and lazy sequences.
Error Handling & Files
try as an expression, require/check, runCatching, and file access.
Sealed Classes, Value Classes & Exhaustive when
Modeling closed hierarchies and type-safe wrappers with no overhead.
Coroutines & Flow
Concurrency with suspend, launch, async, dispatchers, cancellation, and Flow.
Generics, Delegation & DSLs
Variance, reified, delegated properties, class delegation, and builder DSLs.
Gradle, Tests, Java Interop & Multiplatform
Setting up a project, testing, and mixing Kotlin with Java and other platforms.
At a glance
More about Kotlin
Test & practice
Interactive quiz, timed exam with certificate, and exercises with an editor: