Skip to content

e5l/kotlin-by-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

  1. Hello World
  2. Values
  3. Variables
  4. Basic Types & Numbers
  5. Type Checks & Casts
  6. Strings & Templates
  7. Null Safety
  8. Equality

Control Flow

  1. If & When
  2. Ranges & Progressions
  3. Loops & Labels
  4. Functions
  5. Default & Named Arguments
  6. Varargs
  7. Infix Functions
  8. Recursion & tailrec
  9. Lambdas & Higher-Order Functions
  10. Closures

Collections

  1. Arrays
  2. Lists
  3. Sets
  4. Maps
  5. Collection Operations
  6. Grouping & Aggregation
  7. Sequences

Types & OOP

  1. Classes & Constructors
  2. Properties & Accessors
  3. Inheritance & Overriding
  4. Abstract Classes
  5. Interfaces
  6. Visibility Modifiers
  7. Data Classes
  8. Enum Classes
  9. Sealed Classes & Interfaces
  10. Objects & Companion Objects
  11. Nested & Inner Classes
  12. Extension Functions & Properties
  13. Generics & Variance
  14. Reified Type Parameters
  15. Type Aliases
  16. Inline Value Classes
  17. Operator Overloading
  18. Annotations
  19. Reflection

Functional & Advanced

  1. Scope Functions
  2. Destructuring Declarations
  3. Class Delegation
  4. Property Delegation
  5. Inline Functions
  6. Exceptions
  7. Result & runCatching

Concurrency

  1. Coroutines Basics
  2. Suspending Functions
  3. async/await & Structured Concurrency
  4. Dispatchers & Context
  5. Channels
  6. Flows

Practicals

  1. Reading & Writing Files
  2. JSON Serialization
  3. Regular Expressions
  4. String Formatting
  5. Sorting & Comparators
  6. Date & Time
  7. Random
  8. Command-Line Arguments
  9. Building a Type-Safe DSL
  10. Testing

Resources

License

Released under CC BY 4.0.

About

Kotlin by Example is a hands-on introduction to Kotlin via annotated example programs

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Contributors

Languages