Kotlin Basics for Android

June 02, 2026 2 min read

Kotlin is concise, safe and fully interoperable with Java. You don't need to master every feature — this topic covers the 20% you use 80% of the time.

Variables and types

val is read-only (like a constant), var can change. Kotlin infers the type, but you can be explicit.

val name: String = "Anand"  // cannot reassign
var count = 0                 // type inferred as Int
count = count + 1

Null safety — Kotlin's superpower

A normal type can never be null. Add ? to allow null, and use ?. (safe call) or ?: (Elvis, a default) to handle it. This kills the dreaded NullPointerException.

var email: String? = null
val length = email?.length ?: 0  // 0 if email is null

Functions

fun add(a: Int, b: Int): Int = a + b
fun greet(name: String = "friend") = "Hello, $name"

Classes and data classes

A data class auto-generates equals, hashCode and toString — perfect for models.

data class User(val id: Int, val name: String)
val u = User(1, "Asha")
println(u.name)

Collections & lambdas

val nums = listOf(1, 2, 3, 4)
val evens = nums.filter { it % 2 == 0 } // [2, 4]
val doubled = nums.map { it * 2 }       // [2,4,6,8]
Tip: Prefer val by default; switch to var only when you truly need to reassign. It makes code safer and easier to reason about.

Summary

You now know variables, null-safety, functions, data classes and collection operations — enough Kotlin to start building UI.