Kotlin Basics for Android Beginners: A Simple Guide
Learn the Kotlin you need for Android: val and var, types, if/else and when, functions, lists, loops, null safety and data classes, with tiny examples.
Every Android app is written in a language, and for modern Android, that language is Kotlin. In this part, you'll learn the Kotlin basics for Android that you'll use every single day — explained simply, with small examples.
New here? Start with Part 1 of the Android for Beginners series.
Where Can I Practice Kotlin?
You have two easy options:
- Online: go to play.kotlinlang.org. You can type and run Kotlin right in your browser. Nothing to install.
- In Android Studio: right-click your package folder → New → Kotlin Class/File → File, name it
Practice, and write amainfunction.
fun main() {
println("I am learning Kotlin!")
}
Click the small green ▶ next to main to run it. println means "print a line" — it shows text in the output window.
Variables: Boxes That Hold Things
A variable is like a labeled box. You put a value in it and use the label to find it later.
val name = "Karim" // val = cannot change
var age = 20 // var = can change
age = 21 // OK
// name = "Rahim" // Error! val cannot change
Easy rule: use val by default. Only use var when the value really needs to change.
Types: What Kind of Thing Is in the Box?
val city: String = "Dhaka" // text
val students: Int = 30 // whole number
val price: Double = 99.5 // number with decimals
val isOnline: Boolean = true // true or false
Kotlin is smart. Most of the time it guesses the type for you, so you can just write val city = "Dhaka".
String Templates: Mixing Text and Values
Put a $ before a variable to use it inside text:
val name = "Karim"
val age = 21
println("$name is $age years old")
// Karim is 21 years old
You will use this all the time in Android to show text on screen.
If / Else: Making Decisions
val marks = 75
if (marks >= 80) {
println("A+")
} else if (marks >= 60) {
println("Good job")
} else {
println("Keep trying")
}
The app checks each condition from top to bottom and runs the first one that is true.
When: A Cleaner If/Else
When you have many choices, when is easier to read:
val day = 5
val dayName = when (day) {
1 -> "Saturday"
2 -> "Sunday"
5 -> "Wednesday"
else -> "Some other day"
}
println(dayName) // Wednesday
Functions: Reusable Mini-Machines
A function is a mini-machine. You give it something, it does work, and it can give something back.
fun greet(name: String): String {
return "Hello, $name!"
}
println(greet("Nadia")) // Hello, Nadia!
fun= "this is a function"name: String= the input: Stringafter the brackets = the output type
Remember @Composable fun Greeting(...) from Part 3? It's just a function too — one that draws UI!
Lists: Many Things Together
val fruits = listOf("Mango", "Banana", "Jackfruit")
println(fruits[0]) // Mango (counting starts at 0!)
println(fruits.size) // 3
Need a list you can change? Use mutableListOf:
val cart = mutableListOf("Rice")
cart.add("Eggs")
println(cart) // [Rice, Eggs]
Loops: Doing Something Many Times
for (fruit in fruits) {
println("I like $fruit")
}
This prints one line for each fruit. In Android, lists and loops help you show things like messages, products, or contacts.
Null Safety: Kotlin's Superpower
Sometimes a box is empty. In Kotlin, "empty" is called null. Kotlin protects you from crashes caused by empty boxes.
var nickname: String? = null // the ? means "can be empty"
println(nickname?.length) // prints null instead of crashing
println(nickname ?: "No nickname") // No nickname
?after a type = "this can be null"?.= "only do this if it's not null"?:= "if it's null, use this instead"
This one feature prevents many of the crashes that older Android apps had.
Classes: Your Own Types
A data class is a simple way to group related information:
data class Student(val name: String, val age: Int)
val s = Student("Tania", 19)
println(s.name) // Tania
println(s) // Student(name=Tania, age=19)
We will use data classes in Part 7 to show a list of items.
Frequently Asked Questions
Do I need to master Kotlin before building apps? No! These basics are enough to start. You'll learn more naturally while building apps.
Is Kotlin better than Java for Android? For new projects, yes. Google recommends Kotlin, and Jetpack Compose only works with Kotlin.
What should I learn after the basics? Once you're comfortable, look into coroutines for background work. I wrote a guide: Kotlin Coroutines: From Basics to Advanced.
Quick Recap
valfor values that don't change,varfor ones that do.if,elseandwhenmake decisions.- Functions are reusable mini-machines.
- Lists hold many items, and loops go through them.
?means a value might be null, and Kotlin keeps you safe from it.
Now you know enough Kotlin to build real screens. Let's do it in Part 5: Build Your First Screen with Jetpack Compose.