Kotlin Basics for Android Beginners — Part 2

Kotlin Basics for Android Beginners — Part 2

Continuing from Part 1 — this covers the rest of the Kotlin syntax you'll see constantly once the Android course gets into real screens and data.

Classes and objects

class Person(val name: String, var age: Int) {
    fun greet() {
        println("Hi, I'm $name")
    }
}

val person = Person("Ahmad", 25)
person.greet()
person.age = 26

Properties declared in the constructor (val name: String) are available directly on the object — no separate getter/setter boilerplate needed like in Java.

Data classes — the workhorse of Android app data

data class User(val id: String, val name: String, val email: String)

val user = User("1", "Ahmad", "[email protected]")
println(user)                 // auto-generated readable toString()
val copy = user.copy(name = "Sam")  // copy with one field changed

Every model you'll define in Android — a user, a post, an API response — will almost always be a data class. It automatically generates toString(), equals(), and a handy copy() function for free.

Collections: lists, sets, and maps

val names = listOf("Alice", "Bob", "Carol")     // read-only list
val mutableNames = mutableListOf("Alice")        // can add/remove
mutableNames.add("Bob")

val ages = mapOf("Alice" to 30, "Bob" to 25)
println(ages["Alice"])   // 30

Lambdas and higher-order functions

val names = listOf("Alice", "Bob", "Carol")

val upper = names.map { it.uppercase() }
val short = names.filter { it.length <= 3 }
names.forEach { println(it) }

A lambda is just a small, unnamed function — { it.uppercase() } takes each item (referred to as it) and transforms it. You'll use map, filter, and forEach constantly when working with lists of data from an API or database.

Extension functions

fun String.shout() = this.uppercase() + "!"

println("hello".shout())  // HELLO!

This lets you add new functions to existing types (even ones you don't own, like String) without inheriting from them — Android and Compose libraries use this pattern everywhere.

You're ready

That's genuinely enough Kotlin to start. Everything else — coroutines, sealed classes, generics — you'll pick up naturally as the course introduces them in context, which is a far better way to learn them than up front in the abstract.

Start the course: Android Beginners — the full 30-part course