Kotlin
Beginner
1 min read
The Kotlin Type System and Null
Example
fun divide(a: Int, b: Int): Int? {
return if (b == 0) null else a / b
}
// Type hierarchy demonstration
fun printType(value: Any?) {
when (value) {
null -> println("null")
is Int -> println("Int: $value")
is String -> println("String: $value")
is Boolean -> println("Boolean: $value")
else -> println("Other: $value")
}
}
// Function that never returns (return type = Nothing)
fun fail(message: String): Nothing {
throw IllegalStateException(message)
}
fun main() {
val result1 = divide(10, 2)
val result2 = divide(10, 0)
println(result1) // 5
println(result2) // null
// Compiler enforces null checks
val r = result1 ?: fail("Division failed")
println("Result: $r") // 5
printType(42)
printType("hello")
printType(null)
printType(3.14)
// Nullable collections vs collection of nullable
val list1: List<Int?> = listOf(1, null, 3) // list of nullable Int
val list2: List<Int>? = null // nullable list
val nonNull = list1.filterNotNull()
println(nonNull) // [1, 3]
}