Thuta Learning
BasicMobile Developmentintermediate

Kotlin Quick Refresher for Android

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Understand the Kotlin Quick Refresher for Android without any of the intimidation
  • Be able to run Android Studio/Compose code yourself
  • Apply this concept immediately in a real project

Let's think about it for a second

You'll use Null Safety (`?`, `?:`, `!!`) constantly in Android development — it's essential for guarding against crashes with nullable view references. Lambda expressions (`{ ... }`) show up everywhere, in button click listeners and list transformations. Data classes (`data class User(val name: String, val age: Int)`) are used to represent your app's data models (user, product, todo item) — Compose UI state is typically shaped using data classes like these.

Let's connect this to a real-world scenario

Writing `var name: String? = null` tells the compiler that 'name' might be null — writing `name?.length` means that if name happens to be null, you get null back instead of a crash. You'll see button clicks written as lambdas over and over, like `Button(onClick = { println("clicked") })`, once we get to Compose in the next lesson.

Let's look at an example together

kotlin
// Null safety
var name: String? = null
val length = name?.length ?: 0  // 0 if name is null

// Lambda
val greet = { userName: String -> "Hello, $userName!" }

// Data class — will represent our UI state later
data class TodoItem(
    val id: String,
    val title: String,
    val isDone: Boolean = false
)
You should see
$ println(length)
0
$ println(greet("Aye Aye"))
Hello, Aye Aye!

Try it in 5 minutes

Write your own `TodoItem` data class, create 2-3 instances, and inspect the output with `println` (you can use the Kotlin Playground or an Android Studio scratch file).

A quick word of caution

Keep in mind that `!!` is basically forcing the compiler to say 'this thing that could be null definitely isn't' — if it actually turns out to be null, your app will crash immediately.

Easy traps

  • Overusing `!!` (the non-null assertion) in places where a value could actually be null — this can cause a runtime crash (NullPointerException)
  • Confusing a data class with a regular class (one where you write the constructor/getters/setters by hand) — a data class auto-generates `equals`/`toString`/`copy` for you

Now try it yourself

Write your own `TodoItem` data class, create 2-3 instances, and inspect the output with `println` (you can use the Kotlin Playground or an Android Studio scratch file).

You'll know it worked when: $ println(length) 0 $ println(greet("Aye Aye")) Hello, Aye Aye!

Kotlin Quick Refresher for Android | Thuta Learning