thecodex.expert · The Codex Family of Knowledge
Track 1 — Foundations

Kotlin: Foundations

Five lessons: core syntax, null safety, data classes, collections, and extension functions.

🕐 ~4 hours5 lessonsKotlin 2.x

Lessons

Lesson 1

Variables, types, and control flow

val vs var, type inference, String templates, if as expression, when as expression, for and while loops.

What to look for:

  • val = immutable reference (prefer); var = mutable reference
  • Type inference: val name = "Alice" — no type annotation needed
  • String templates: "Hello, $name!" and "Length: ${name.length}"
  • when is Kotlin's switch — but it's an expression that returns a value
🕑 45 min
Lesson 2

Null safety: String vs String?, ?. and ?:

Non-nullable types, nullable types (T?), safe call (?.), Elvis operator (?:), smart casts, and !! (when to avoid).

Prerequisite: Lesson 1

What to look for:

  • String cannot hold null — String? can; the difference is at compile time
  • ?. short-circuits the chain: a?.b?.c returns null if any step is null
  • ?: provides a fallback: name ?: "Unknown" returns "Unknown" if name is null
  • !! is an explicit crash request — avoid in production code
🕑 45 min
Lesson 3

Data classes and objects

data class (auto-generated equals/hashCode/copy), primary constructors, object declarations (singletons), and companion objects.

Prerequisite: Lesson 2

What to look for:

  • data class generates: equals, hashCode, toString, copy, componentN — one line vs 50 in Java
  • copy() creates a modified version: user.copy(age = 31) — original unchanged
  • object MyObject — a singleton: one instance, created lazily on first access
  • companion object — a class's static members equivalent in Kotlin
🕑 45 min
Lesson 4

Collections: listOf, mapOf, filter, map

Read-only vs mutable collections, listOf/mutableListOf, filter/map/reduce/groupBy/flatMap, and sequences for lazy evaluation.

Prerequisite: Lesson 3

What to look for:

  • listOf() returns read-only; mutableListOf() returns MutableList
  • Collections API is lazy with sequences: .asSequence().filter{...}.map{...}.toList()
  • it: the implicit lambda parameter name for single-argument lambdas
  • groupBy returns Map<K, List<V>> — useful for categorising data
🕑 45 min
Lesson 5

Functions: lambdas, higher-order, and extensions

Lambda syntax, higher-order functions, function types (String) -> Int, trailing lambdas, and extension functions.

Prerequisite: Lesson 4

What to look for:

  • Trailing lambda: if last arg is a lambda, move it outside: list.filter { it > 0 }
  • Function type: (Int, Int) -> Int — a function that takes two Ints and returns Int
  • Extension functions resolve statically — they can't override member functions
  • inline keyword on extension functions avoids lambda object creation overhead
🕑 45 min

How to use this track: Each lesson links to the Kotlin reference pages. Practice at play.kotlinlang.org — the official Kotlin Playground with examples.

Track quiz

4 questions to check your understanding.

What is the difference between val and var in Kotlin?

What does name?.length return if name is null?

What does data class User(val name: String, val age: Int) automatically generate?

What is the difference between listOf() and mutableListOf()?

Practitioner track →