Head First Kotlin cover

Better than initially assessed: Chapter 2 introduces collections, good variance coverage (with a Pet hierarchy), covers extensions and their role in the stdlib, two lambda/HOF chapters. Unusual ordering: generics before lambdas. map/filter/fold deferred to lambda chapters. Coroutines appendix exists but dated.

Head First Kotlin uses the usual Head First spiral approach (introduce a concept lightly, let it sit, circle back with depth later), which meant it deferred anything not strictly load-bearing to the appendices. Labeled breaks and enum classes, both things I’d consider core language features, got pushed all the way to Appendix C rather than covered in the main chapters. That appendix is even honest about it in the title: “The Top Ten Things (We Didn’t Cover)”: ten separate topics, including packages, visibility modifiers, sealed classes, and object declarations, that never made it into the twelve main chapters at all.

The early chapters lay down Kotlin’s foundational mechanics: null safety, safe calls, and the Elvis operator to banish NullPointerExceptions from day-to-day code:

// Chapter 8 tackles Kotlin's null safety system and nullable types:
class Duck(val name: String?)

fun printDuckName(duck: Duck?) {
    // Safe calls chain without throwing NullPointerException:
    val length = duck?.name?.length ?: 0
    println("Duck name length: $length")

    // Executing code only when a value is guaranteed non-null:
    duck?.name?.let {
        println("Valid duck name: $it")
    }
}

The generics chapter is surprisingly thorough. Using a concrete Pet hierarchy with dogs and cats, it walks through declaration-site variance and demystifies what out (covariance) and in (contravariance) mean in practice:

// Chapter 10 uses a Pet hierarchy to teach generic variance (in/out):
abstract class Pet(val name: String)
class Cat(name: String) : Pet(name)
class Dog(name: String) : Pet(name)

// Covariance (out) allows Contest<Cat> to be passed where Contest<Pet> is expected:
class Contest<out T : Pet>(val entries: List<T>) {
    fun getWinner(): T = entries.random()
}

// Contravariance (in) allows a Retailer<Pet> to supply animals to a Retailer<Cat>:
interface Vet<in T : Pet> {
    fun treat(patient: T)
}

The book finishes strong with two dedicated chapters on lambdas, higher-order functions, and standard collection combinators, illustrating trailing lambda syntax and pipeline operations:

// Chapters 11 and 12 close out with higher-order functions and collection combinators:
data class Grocery(
    val name: String,
    val category: String,
    val unitPrice: Double,
    val quantity: Int
)

fun search(groceries: List<Grocery>, criteria: (Grocery) -> Boolean): List<Grocery> {
    val results = mutableListOf<Grocery>()
    for (item in groceries) {
        if (criteria(item)) {
            results.add(item)
        }
    }
    return results
}

fun main() {
    val cart = listOf(
        Grocery("Tea", "Beverage", 4.5, 2),
        Grocery("Coffee", "Beverage", 8.0, 1),
        Grocery("Apples", "Produce", 1.2, 5)
    )

    // Trailing lambda syntax and chaining stdlib combinators:
    val totalProduceCost = cart
        .filter { it.category == "Produce" }
        .map { it.unitPrice * it.quantity }
        .fold(0.0) { acc, cost -> acc + cost }
}

Compare that to Head First Go, which felt reasonably complete without needing to punt a top-ten list to the back of the book, but that’s not really a fair fight. Go is a deliberately small language by design (that’s Rob Pike’s whole philosophy: minimal surface area, few ways to do anything), so a Head First treatment of it has a real shot at covering the whole language in the main chapters. Kotlin’s surface area is just bigger: null safety, data classes, sealed classes, extension functions, coroutines, object expressions/declarations, operator overloading. So a book aimed at teaching it from scratch in a similarly-sized page count is going to hit a wall and have to defer something. It’s less “Head First Kotlin did a worse job” and more “the two books picked structurally different-sized languages to cover with the same format.”