Free Handbook · Every example compiled & verified

Kotlin Handbook

Learn Kotlin the way it is written at work, on the JVM and on Android. Seventeen modules from your first program to a job: every example program was compiled with kotlinc and run on the JVM before publishing, with its output shown exactly as it printed, and the real compiler and runtime errors you will hit are explained with the fix. Variables and types, flow control, functions and lambdas, null safety, collections and sequences, classes, inheritance and interfaces, data, sealed and enum classes, generics and extension functions, coroutines, debugging, Kotlin with Gradle, JUnit 5, Ktor, Spring Boot, PostgreSQL, Android, Jetpack Compose, Docker and Git, data structures and algorithms, problem-solving patterns, interview questions, a certification exam, and a resume check at the end.

0 / 136 lessons🔥 0 day streak
ShareXLinkedIn

How this handbook works

Every lesson has a complete Kotlin program with the exact output it prints — each one compiled and run with the real Kotlin toolchain before it was published, so what you see is what you will get when you run it yourself — a trace of what the program did line by line where that matters, and the real compiler or runtime error you will hit next, with why it happened and the fix. Lessons tick themselves as you go. Finish the handbook, sit the exam, get the certificate, then take your resume through the ATS checker against a real job posting.

17modules
136lessons
121compiled examples
58real errors explained

Module 00 · what you'll be able to do

  • Explain what Kotlin is, how it compiles to JVM bytecode, and why it works with every Java library
  • Install Kotlin through IntelliJ IDEA, Android Studio or the kotlinc command-line compiler on macOS, Windows or Linux
  • Compile a Kotlin file to a runnable JAR and run it with java -jar, and create a Gradle project for real work
  • Read the structure of a Kotlin file: fun main, statements, blocks, comments and string templates
  • Use this handbook, its verified examples and an AI assistant without shortcutting your own learning
01

Why learn Kotlin

Kotlin is a statically typed language made by JetBrains, the company behind IntelliJ IDEA. It compiles to the same JVM bytecode Java compiles to, so a Kotlin program runs anywhere Java runs and can call any Java library — Spring, Hibernate, Jackson, JDBC drivers — without wrappers. It was designed to fix the parts of Java that cause the most bugs and the most typing: null, boilerplate getters and setters, verbose lambdas, and checked exceptions.

In 2019 Google made Kotlin the preferred language for Android, and today almost every new Android app and most Android job listings are Kotlin-first. On the server side, Kotlin runs on Spring Boot (which has first-class Kotlin support) and on Ktor, JetBrains' own web framework. Kotlin Multiplatform lets one codebase share business logic between Android, iOS, desktop and the web.

Where Kotlin is usedWhat you buildTypical stack
AndroidPhone, tablet, TV and Wear OS appsKotlin, Jetpack Compose, coroutines, Room, Retrofit
Backend servicesREST and gRPC APIs, microservicesSpring Boot or Ktor, PostgreSQL, Kafka, Docker
MultiplatformShared logic for Android + iOS + webKotlin Multiplatform, Compose Multiplatform
Tooling and build scriptsGradle build logic, internal CLIsGradle Kotlin DSL (build.gradle.kts)
Data engineeringJVM data jobs and stream processorsKafka clients, Apache Spark (via the Java API), Flink

Java

  • Any reference can be null; a NullPointerException is found at runtime
  • A data class needs a constructor, getters, equals, hashCode, toString (or a record)
  • Semicolons required; types written out or var for locals
  • Checked exceptions must be caught or declared
  • Huge ecosystem, the default for enterprise backends

Kotlin

  • Types are non-null by default; String? must be handled before use, checked at compile time
  • data class User(val name: String) — one line, all of that generated
  • No semicolons; types inferred almost everywhere
  • No checked exceptions
  • Uses the same ecosystem — Kotlin and Java files live side by side in one project
Kotlin and Java in real jobs
Most companies that "use Kotlin" also have Java code, often in the same repository. You will read Java even if you only write Kotlin, which is why this handbook points out the Java equivalent where it helps. If you know Java already, Kotlin will feel like Java with the sharp edges filed off; if you do not, you lose nothing by starting here.
02

Install Kotlin

There are two sensible ways to get Kotlin. For learning and real projects, install an IDE — it bundles the compiler, runs your code with one click, and underlines errors as you type. For the terminal (and to understand what the IDE does for you), install the command-line compiler kotlinc. Either way you need a JDK (Java Development Kit) because Kotlin runs on the JVM — JDK 17 or 21 is a safe choice.

  1. 1
    1. IntelliJ IDEA (recommended for learning and backends)

    Download IntelliJ IDEA (the free Community Edition is enough) from jetbrains.com. Choose New Project → Kotlin, pick a JDK (the wizard can download one), and it creates a project with a Main.kt. Click the green arrow next to fun main to run it.

  2. 2
    2. Android Studio (for Android apps)

    Android Studio is IntelliJ IDEA with Android tools added. It ships with Kotlin and its own JDK. Use it if your goal is Android; everything in Modules 01–11 works there too, in a plain Kotlin file.

  3. 3
    3. kotlinc on the command line

    Install the compiler with a package manager (commands below), then compile with kotlinc and run the result with java. Every example in this handbook was verified exactly this way.

bash
brew install openjdk@21
brew install kotlin
kotlinc -version
java -version

macOS — Homebrew installs kotlinc; install a JDK first if java -version fails.

bash
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install java 21-tem
sdk install kotlin
kotlinc -version

Linux (and macOS, WSL) — SDKMAN installs and switches Java and Kotlin versions per user.

powershell
winget install EclipseAdoptium.Temurin.21.JDK
scoop install kotlin
kotlinc -version

Windows — winget for the JDK, then Scoop (or the zip from the Kotlin GitHub releases page, added to PATH) for kotlinc.

Which Kotlin version?
This handbook teaches Kotlin 2.x, which uses the new K2 compiler. Its error messages are what you see in the error cards. Anything from Kotlin 1.9 onwards runs every example here; newer versions only add features.
03

Your first Kotlin program

Save this as Main.kt. A Kotlin program starts at a top-level function called main — no class needed, unlike Java.

kotlinMain.kt
fun main() {
    println("Hello, Kotlin!")
    val language = "Kotlin"
    val year = 2011
    println("$language was first announced in $year.")
}
Outputcompiled & run with real Kotlin
Hello, Kotlin!
Kotlin was first announced in 2011.
Your turn

Add a third line that prints your own name with a string template: val name = "..." then println("Hi, $name").

Compile it to a self-contained JAR and run that JAR with Java. -include-runtime bundles the small Kotlin standard library into the JAR so plain java can run it:

bash
kotlinc Main.kt -include-runtime -d app.jar
java -jar app.jar

The first compile takes a few seconds — kotlinc itself runs on the JVM and has to start up.

VisualizeWhat the program didStep 1 / 6
fun main() {
println("Hello, Kotlin!")
val language = "Kotlin"
val year = 2011
println("$language was first announced in $year.")
}
Line 1

The JVM starts, loads the class the compiler generated for Main.kt (it is called MainKt) and calls its main function.

Variables now

nothing yet

All 6 steps as a table
StepLineWhat happenedVariables now
11The JVM starts, loads the class the compiler generated for Main.kt (it is called MainKt) and calls its main function.
22println writes the string and a newline to standard output.
33val declares a read-only variable. The type String is inferred from the value.language = "Kotlin"
44Another val, inferred as Int.language = "Kotlin" year = 2011
55$language and $year inside the string are string templates — replaced by the variables' values.
66main returns, and the program exits with status 0.
Error you will hit

Unresolved reference: a typo in a name

kotlin
fun main() {
    val language = "Kotlin"
    println(langauge)
}
Main.kt:3:13: error: unresolved reference 'langauge'.
Why the compiler said that

Kotlin checks every name at compile time. langauge was never declared, so the compiler refuses to produce a program at all — nothing runs.

The fix

Spell the name exactly as declared. Names are case-sensitive: Language and language are different.

kotlin
fun main() {
    val language = "Kotlin"
    println(language)
}
04

How a Kotlin file is structured

A Kotlin file can hold functions, classes and variables directly at the top level. Statements end at the end of the line — semicolons are allowed but nobody writes them. Curly braces group statements into blocks; indentation is only for humans. Comments come in three shapes.

kotlinMain.kt
// A single-line comment runs to the end of the line.

/* A block comment
   can span several lines. */

/**
 * A KDoc comment documents the declaration below it.
 * @return the square of n
 */
fun square(n: Int): Int = n * n

fun main() {
    val sides = 4
    println("A square has $sides sides")
    println("4 squared is ${square(4)}")  // ${} holds any expression
}
Outputcompiled & run with real Kotlin
A square has 4 sides
4 squared is 16
Your turn

Add a function cube(n: Int): Int above main and print cube(3) with a ${} template.

PieceWhat it is
fun main()The entry point. Can also be fun main(args: Array<String>) to read command-line arguments.
fun square(n: Int): IntA function: name, parameters with types after a colon, return type after the parentheses.
val / varRead-only / reassignable variable (Module 01).
"$x", "${expr}"String templates: insert a variable or any expression into a string.
package, importOptional first lines of a file: which package the file belongs to, and what it uses from other packages.
kotlinc
The Kotlin command-line compiler. Turns .kt files into JVM bytecode (.class files or a JAR).
JVM
Java Virtual Machine — the runtime that executes bytecode. Kotlin and Java both compile to it.
JDK
Java Development Kit — the JVM plus tools. Kotlin needs one installed to run.
Bytecode
The portable instruction format in .class files, run by the JVM on any OS.
Kotlin standard library
kotlin-stdlib: collections, strings, ranges, println and more. Bundled into a JAR with -include-runtime.
Gradle
The build tool almost every Kotlin project uses; build files are written in Kotlin (build.gradle.kts).
String template
A $name or ${expression} inside a string literal, replaced by its value.
Kotlin Multiplatform
Kotlin compiled to JVM, native (iOS) and JavaScript/Wasm targets from shared source code.
05

A real project: Gradle

One file and kotlinc are perfect for learning. Real projects have many files and dependencies, and they use Gradle to compile, test and package them. IntelliJ IDEA creates a Gradle project for you; from the terminal, gradle init does the same. The build file is itself written in Kotlin:

kotlinbuild.gradle.kts
plugins {
    kotlin("jvm") version "2.4.20"
    application
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation(kotlin("test"))
}

kotlin {
    jvmToolchain(21)
}

application {
    mainClass.set("MainKt")
}

mainClass is MainKt because top-level functions in Main.kt are compiled into a class named after the file.

bash
gradle init --type kotlin-application   # scaffold a project
./gradlew run                            # compile and run
./gradlew test                           # run the tests
./gradlew build                          # produce the JAR in build/libs
Use the wrapper
Always run ./gradlew (the Gradle wrapper committed to the repository) rather than a globally installed gradle. The wrapper pins the Gradle version so every developer and the CI server build the same way.
06

How this handbook works

Seventeen modules, each its own page, from val to a job-ready resume. The rail on the left lists them; the bar at the top and the pill at the bottom-left track your progress. A lesson ticks itself when you reach its end, and the tick survives closing the tab — no account needed.

  • Examples are compiled and verified. Kotlin cannot run inside a web page, so every example here is a complete program that was compiled with the real Kotlin compiler and run on a JVM before publishing. The output under it is exactly what it printed — paste the program into your IDE, or save it as Main.kt and run the two commands above, to see the same result.
  • "Your turn" tasks under each example ask you to change one thing. Predict the output before you run it — that is the fastest way to learn.
  • Traces step through a program line by line with the variables at each step — used where loops, recursion and dispatch get confusing.
  • Error cards show the real kotlinc or JVM message, why it happened, and the fix. Module 11 collects the fifteen most common.
  • Interview questions appear where the topic is taught and are collected in Module 15, tiered junior, mid and senior.
  • The handbook ends with a certification exam (25 questions, 70% to pass), then the ATS resume checker with a real job posting.
ModulesYou learn
01–03val and var, types, strings, input, if and when, loops, functions, lambdas, higher-order functions
04–05Null safety (the feature Kotlin is famous for), lists, sets, maps and the collection operations
06–09Classes, constructors, objects, inheritance, interfaces, data, sealed and enum classes, generics, extension functions
10Coroutines: suspend functions, structured concurrency, Flow
11–14Errors and debugging, Kotlin with Gradle, Ktor, Spring, Android and Docker, data structures and algorithms, problem solving
15–16Sixty interview questions, portfolio projects, resume, jobs and the exam
The floating board
The pill at the bottom-left opens a board with every key term from the module you are on and a tutor you can ask about the code on the page. It is a language model — check what it says against a real compiler.
07

Using AI to learn (without letting it learn for you)

An AI assistant can write a working Kotlin function in seconds, and so can the one built into your IDE. That is why you should type the code yourself while learning: the goal is not the code, it is the model in your head of what the compiler checks and what the program does — the thing an interviewer probes when they ask why !! is a smell or when a Sequence beats a List.

Do this

  • Paste a real kotlinc error and ask what it means — then fix it yourself
  • Ask "why is this String? and not String?"
  • Write your solution first, then ask for a review of idiomatic Kotlin
  • Ask it to explain a scope function (let, apply) line by line

Not this

  • Ask for the answer to a "Your turn" task and paste it in
  • Silence a null error with !! because the AI suggested it
  • Copy a Java answer that the IDE auto-converted without reading it
  • Use it during an interview (they can tell)

Ready? Module 01 starts with the fundamentals: val and var, the basic types, strings and templates, operators, reading input and converting between types.

Every lesson in the handbook

17 modules · 136 lessons · each one ticks itself when you reach the end.

00Getting Started0 / 7
01Fundamentals0 / 7
02Flow Control0 / 6
03Functions & Lambdas0 / 7
04Null Safety0 / 7
05Collections0 / 7
06Classes & Objects0 / 6
07Inheritance & Interfaces0 / 7
08Data, Sealed & Enum Classes0 / 7
09Generics & Extensions0 / 7
10Coroutines0 / 8
11Errors & Debugging0 / 8
12Kotlin + Tools0 / 10
13Data Structures & Algorithms0 / 13
14Problem Solving0 / 11
15Interview Questions0 / 12
16Job Ready0 / 6

Frequently asked questions

Is this Kotlin handbook free?
Yes — every module, every example and the certification exam are free, with no signup and no paywall. The exam asks you to sign in with Google so the certificate can carry your name.
Do I need to know Java before learning Kotlin?
No. Module 01 starts from variables and types and assumes nothing. If you already know Java you will move faster, and the handbook calls out where Kotlin differs, such as == comparing content, classes being final by default, no implicit widening from Int to Long, and null safety in the type system.
Do I need to install anything?
Yes — a JDK and the Kotlin compiler, or IntelliJ IDEA or Android Studio, which include Kotlin. Module 00 shows how to install them and run your first program. The examples on this page do not run in the browser; instead, each example program was compiled with kotlinc and run on the JVM before publishing, and the output shown is exactly what it printed, so you can paste it into Main.kt and get the same result. Code that needs a library is not verified that way: the coroutine code that uses kotlinx.coroutines (launch, async, delay, Flow) and the framework labs in Module 12 are shown as clearly marked snippets, with their output described rather than run.
Which version of Kotlin does this handbook use?
Kotlin 2.x with the K2 compiler, whose error messages are the ones shown in the error cards. Anything from Kotlin 1.9 onwards runs every example; newer versions only add features. Examples run on a current JDK, and Module 12 shows how Gradle pins the Kotlin and Java versions for a real project.
Is there a certificate?
Yes. Pass the 25-question exam (35 minutes) with 70% or more and generate a SolutionGigs Kotlin certificate with your name, score, date and a certificate ID — download it, print it or add it to LinkedIn.
Is Kotlin better than Java?
For new code, Kotlin removes most null-pointer bugs at compile time and needs far less boilerplate, which is why Android and many backend teams moved to it. Java has the larger job market and is not going away. Because they share the JVM and call each other freely, learning one makes the other easy — and most Kotlin jobs expect you to read Java too.
Do I need Android Studio to learn Kotlin?
No. Android Studio is for Android apps. For learning the language, IntelliJ IDEA Community Edition (free) or just kotlinc plus a text editor is enough. Everything in Modules 01–11 is plain Kotlin that runs on any JVM.
How do I run a Kotlin file from the command line?
Compile it with kotlinc Main.kt -include-runtime -d app.jar and run it with java -jar app.jar. The -include-runtime flag bundles the Kotlin standard library into the JAR so plain Java can run it.

Finish the Kotlin handbook, then get hired

Sit the exam for your certificate, run your resume through the ATS checker, and see the jobs that ask for exactly this.

Check my resume
Found this course useful? Share it.
ShareXLinkedIn

Comments

0

Join the conversation. Sign in to leave a comment — we'd love to hear your thoughts.