Kotlin Intermediate Introduction
Kotlin Intermediate is the second band of Kotlin courses. It treats functions as values and goes deeper into collections, classes, and null safety.
The second band at a glance
A variable still holds a value and println still shows it. The programs keep the beginner shape.
fun main() {
val course = "Kotlin Intermediate"
val lessons = 37
println(course)
println(lessons)
}A list walked with a lambda
forEach runs its lambda once for each topic in the list. The lambda prints the topic it is handed.
fun main() {
val topics = listOf("Functions", "Collections", "Classes")
topics.forEach { println(it) }
}Choices as expressions
An if expression picks one value and a when expression picks another. Both results sit inside variables.
fun main() {
val score = 82
val grade = if (score >= 90) "A" else if (score >= 70) "B" else "C"
println(grade)
val label = when {
score >= 90 -> "Top"
score >= 70 -> "Strong"
else -> "Growing"
}
println(label)
}Example
fun main() {
val names = listOf("Ari", "Bea", "Cid")
names.forEach { name ->
val greeting = if (name == "Bea") "Welcome back" else "Welcome"
println(greeting + ", " + name)
}
}Key points
- The beginner course covered variables, strings, lists, loops, and classes.
- This course builds on those same shapes and adds depth to each one.
- Functions become values that a program can store and pass around.
- Collections and classes gain stronger tools in this band.
- Null safety gains sharper checks for missing values.
- Every program in this course is still a complete main.kt file.
In this example, the list holds three names. forEach hands each name to the lambda in turn. The if expression picks a greeting for each name, and println shows the greeting with the name.
Note
Each lesson here uses only shapes from earlier lessons or its own new shape.
Try it Yourself
Edit the code, then press Run.
Running is simulated Β· Kotlin Playground
Exercise
What is new about functions in this band?