All days
Day 02Module 1 · Kotlin Basics· 3–5 hrs

Kotlin Basics II — Collections, Functions, Lambdas, Null Safety

Arrays and lists, functions (including higher-order ones), lambdas, and Kotlin's famous null safety. This is where Kotlin starts to feel powerful and expressive.

0% done

Key concepts

  • Array vs ArrayList vs List
  • Function declarations & default arguments
  • Higher-order functions
  • Lambdas & trailing lambda syntax
  • map / filter / forEach
  • Nullable types (?), safe call (?.), Elvis (?:)

Lectures in this day

  1. 01

    Array and ArrayList

    An Array is a row of boxes with a size you decide at the start — you can't add more boxes later. An ArrayList is a stretchy row that grows and shrinks as you add or remove things. Use `listOf(...)` when you only need to read the list.

    Real life · An egg tray with 30 slots — that's an Array (fixed size). A shopping bag you keep adding items to — that's an ArrayList (grows).
    kotlin
    0
    val eggs = IntArray(30) { it + 1 }   1
    
    2
    val cart = arrayListOf("Milk", "Bread")
    cart.add("Eggs")
    cart.remove("Bread")
    println(cart)   3

    Practice · 3 quick questions

    1. Q1.Which is fixed size?

    2. Q2.Which one supports `add()` to grow?

    3. Q3.First index in an array is…

    Pick one answer per question.
  2. 02

    Functions in Kotlin

    A function is a named piece of code you can run any time. You give it inputs (parameters) and it usually gives back a result. Parameters can have default values, and you can pass them by name so the order does not matter.

    Real life · A coffee machine: press one button and it makes a latte. Change a setting (size = large) to get a bigger cup — same machine, different input.
    kotlin
    0
    fun latte(size: String = "M", shots: Int = 1): String =
        "$size latte with $shots shot(s)"
    
    println(latte())                       1
    println(latte(shots = 2))              2
    println(latte(size = "L", shots = 3))  3

    Practice · 3 quick questions

    1. Q1.Which keyword defines a function?

    2. Q2.In `fun add(a: Int, b: Int): Int`, what is `Int` after the `)`?

    3. Q3.A function with no return uses…

    Pick one answer per question.
  3. 03

    Higher Order Functions and Lambda

    A function can take another function as an input. That inner function is often written inline in curly braces — that short inline function is called a lambda. Inside a lambda, `it` is the name for the single input.

    Real life · A recipe that says 'season to taste'. You pass in the seasoning behavior. Different cook, different lambda, same recipe.
    kotlin
    0
    1
    fun doTimes(times: Int, action: (Int) -> Unit) {
        for (i in 1..times) {
            action(i)   2
        }
    }
    
    3
    doTimes(3) { i -> println("Ring $i") }
    4
    
    5
    val numbers = listOf(1, 2, 3)
    val doubled = numbers.map { it * 2 }   6
    println(doubled)   7

    Practice · 3 quick questions

    1. Q1.A lambda is…

    2. Q2.In `list.map { it * 2 }`, what is `it`?

    3. Q3.A higher-order function is one that…

    Pick one answer per question.
  4. 04

    Null Safety in Kotlin

    'Null' means 'no value at all'. Kotlin splits types into two groups: normal (`String`, always has a value) and nullable (`String?`, may be null). Use `?.` to say 'only call this if it's not null', and `?:` to give a backup value when it IS null.

    Real life · Before you leave home, you check your umbrella. If you have one, you can open it. If not, you use a backup plan (like wearing a hoodie).
    kotlin
    fun greet(name: String?) {   0
        1
        2
        val length = name?.length ?: 0
        println("Hi ${name ?: "stranger"} ($length chars)")
    }
    greet("Ayesha")   3
    greet(null)       4

    Practice · 3 quick questions

    1. Q1.What does `?.` do on `user?.name`?

    2. Q2.What does `?:` (Elvis) do?

    3. Q3.What does `!!` do?

    Pick one answer per question.
  5. 05

    Module 1 Wrap-up / Summary

    You now know the basic pieces of Kotlin: boxes for values, `if`/`when` for choices, loops for repeating, lists for groups of things, functions for reusable steps, and nullable types so empty boxes don't crash your app.

    Real life · You've learned the alphabet and grammar. Starting tomorrow, you write full sentences — with classes and objects.

    Practice · 3 quick questions

    1. Q1.Module 1 was mainly about…

    2. Q2.Which topic was NOT in Module 1?

    3. Q3.You should be comfortable with…

    Pick one answer per question.

Real-life analogies

Higher-order functions

A recipe that takes another recipe as an ingredient: 'blend using this method'. The method itself is what you pass in.

Null safety

A nullable type is a box that might be empty. `?.` asks 'if there's something inside, do this'. `?:` says 'otherwise use this fallback'.

Code examples

ArrayList & higher-order functions

kotlin
data class Task(val title: String, var done: Boolean = false)

val tasks = arrayListOf(
    Task("Buy milk"),
    Task("Study Kotlin", done = true),
    Task("Call mom")
)

val open = tasks.filter { !it.done }
                .map { it.title.uppercase() }

open.forEach(::println)

Null safety

kotlin
fun greet(name: String?) {
    0
    val length = name?.length ?: 0
    println("Hi ${name ?: "stranger"} ($length chars)")
}

greet("Ayesha")
greet(null)

Avoid !! (double-bang). It throws NullPointerException — the very thing null safety exists to prevent.

Hands-on checklist

Milestone

A working, null-safe Kotlin console mini-app.

Common pitfalls

  • Reaching for !! to silence the compiler — use ?. or ?: instead.
  • Using Array<Int> when you meant IntArray (they're different types).