Tutorial 2
Kotlin basics you need before Compose.
You do not need advanced Kotlin to start Android. You need enough to read examples, model screen data, and write small functions safely.
Core concepts
val and varUse val by default. Use var only when the value must change.
Data classA compact way to represent app data such as Habit, User, or Product.
Null safetyKotlin forces you to handle missing values instead of crashing later.
Code walkthrough
data class Habit(
val id: Int,
val title: String,
val isDone: Boolean = false
)
fun completedCount(habits: List<Habit>): Int {
return habits.count { habit -> habit.isDone }
}
fun labelFor(count: Int): String {
return if (count == 1) "1 habit done" else "$count habits done"
}Important beginner habit: read the type of every value. If you know the type, you can usually predict what operations are allowed.
Practice task
- Create a data class named Course with title, durationWeeks, and isCompleted.
- Create a list of three courses.
- Write a function that returns only completed courses.
- Write a function that prints a friendly summary.
Reference: Android Basics with Compose starts with introductory Kotlin concepts before app building.