Kotlin is a modern, expressive programming language developed by JetBrains and officially supported by Google for Android development. It is designed to be concise, safe, and fully interoperable with Java.
Most modern Android apps are now written in Kotlin because it reduces boilerplate code, prevents common runtime errors, and improves developer productivity significantly.
Why Kotlin?
Before Kotlin, Android development was dominated by Java, which often required verbose syntax and lacked modern language features like null safety and coroutines.
Kotlin solves these problems by introducing a cleaner syntax, better type safety, and powerful built-in features that make Android development faster and less error-prone.
Variables in Kotlin
Kotlin provides two ways to declare variables: val for immutable values and var for mutable values.
val name: String = "Alex" // cannot be changed
var age: Int = 25 // can be changedUsing val is recommended by default because immutability makes code safer and easier to debug.
Type Inference
Kotlin can automatically detect variable types, which makes code shorter without losing type safety.
val city = "Delhi" // String inferred
val count = 10 // Int inferredFunctions
Functions in Kotlin are declared using the fun keyword. They can return values or be unit functions (similar to void in Java).
fun greet(name: String): String {
return "Hello, $name"
}Kotlin also supports expression-based functions, making code more compact.
fun greet(name: String) = "Hello, $name"Strings and Templates
Kotlin makes string manipulation easier using string templates.
val name = "Rahul"
println("My name is $name")You can also embed expressions inside strings using curly braces.
val a = 5
val b = 10
println("Sum is ${a + b}")Null Safety
One of Kotlin’s most powerful features is built-in null safety, which helps eliminate NullPointerExceptions at compile time.
var name: String? = nullThe question mark (?) means the variable can hold a null value. Without it, Kotlin enforces non-null values.
Safe Calls and Elvis Operator
Kotlin provides safe operators to handle nullable values gracefully.
val length = name?.length // safe call
val display = name ?: "Unknown" // Elvis operatorControl Flow
Kotlin supports if-else, when expressions, and loops.
val result = if (age > 18) "Adult" else "Minor"The when expression is Kotlin’s powerful replacement for switch-case.
when (age) {
18 -> "Just Adult"
60 -> "Senior"
else -> "Other"
}Collections
Kotlin provides lists, sets, and maps with rich built-in functions.
val list = listOf(1, 2, 3)
val mutableList = mutableListOf(1, 2, 3)Why Kotlin Matters in Android
Kotlin has become the primary language for Android because it reduces boilerplate, integrates seamlessly with Jetpack libraries, and supports modern features like coroutines, flows, and functional programming.
Mastering Kotlin basics is essential before moving into advanced Android topics like architecture, concurrency, and Jetpack Compose.