Kotlin by Example
Kotlin by Example is a hands-on introduction to Kotlin via annotated example programs. It is inspired by Go by Example and zig-by-example: read the code, read the notes, run it yourself.
Kotlin is a concise, statically-typed language for the JVM (also Native, JS, and Wasm). It emphasizes null safety, conciseness, and seamless Java interop, letting you write expressive code that runs anywhere the JVM does and beyond.
Getting started
The examples are run with the Kotlin toolchain and its kotlin command-line tool.
0. Install the toolchain
macOS / Linux:
curl -fsSL https://kotl.in/install.sh | sh
Windows (PowerShell):
powershell -ExecutionPolicy ByPass -c "irm 'https://kotl.in/install.ps1' | iex"
You can also sdk install kotlintoolchain via SDKMAN. Check it works with kotlin --version.
1. Create a scratch project
The kotlin tool runs projects, not loose .kt files. Set up one small project and reuse it for every example:
my-kotlin/
├─ module.yaml
└─ src/
└─ main.kt
module.yaml:
product: jvm/app
2. Run an example
Paste an example’s code into src/main.kt, then run it from the project root:
$ kotlin run
Hello, World!
kotlin run also prints a couple of INFO compiler lines as it builds; add --log-level=warn (kotlin --log-level=warn run) to show only your program’s output, as the examples below do. Pass command-line arguments after --, e.g. kotlin run -- Alice 42.
3. Add dependencies when needed
A few examples (coroutines, serialization, reflection, testing) need extra libraries. Declare them in module.yaml:
product: jvm/app
dependencies:
- org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2
Each example notes which dependency it requires. See the Kotlin toolchain tutorial for more.
Examples
Basics
- Hello World
- Values
- Variables
- Basic Types & Numbers
- Type Checks & Casts
- Strings & Templates
- Null Safety
- Equality
Control Flow
- If & When
- Ranges & Progressions
- Loops & Labels
- Functions
- Default & Named Arguments
- Varargs
- Infix Functions
- Recursion & tailrec
- Lambdas & Higher-Order Functions
- Closures
Collections
Types & OOP
- Classes & Constructors
- Properties & Accessors
- Inheritance & Overriding
- Abstract Classes
- Interfaces
- Visibility Modifiers
- Data Classes
- Enum Classes
- Sealed Classes & Interfaces
- Objects & Companion Objects
- Nested & Inner Classes
- Extension Functions & Properties
- Generics & Variance
- Reified Type Parameters
- Type Aliases
- Inline Value Classes
- Operator Overloading
- Annotations
- Reflection
Functional & Advanced
- Scope Functions
- Destructuring Declarations
- Class Delegation
- Property Delegation
- Inline Functions
- Exceptions
- Result & runCatching
Concurrency
- Coroutines Basics
- Suspending Functions
- async/await & Structured Concurrency
- Dispatchers & Context
- Channels
- Flows
Practicals
- Reading & Writing Files
- JSON Serialization
- Regular Expressions
- String Formatting
- Sorting & Comparators
- Date & Time
- Random
- Command-Line Arguments
- Building a Type-Safe DSL
- Testing
Resources
License
Released under CC BY 4.0.