Basic Types
Kotlin’s type system is statically typed with null safety baked in. Every type is either nullable (T?) or not (T). The compiler will reject null wherever non-nullable.
Common types + null safety
EXAMPLE
fun main() {
// Primitives (boxed when nullable)
val n: Int = 42
val pi: Double = 3.14
val ok: Boolean = true
val ch: Char = 'A'
val msg: String = "hi"
// Nullable counterparts
val maybe: String? = null
println(maybe?.length ?: 0)
// Collections (read-only by default; Mutable* for mutation)
val xs: List<Int> = listOf(1, 2, 3)
val mxs: MutableList<Int> = mutableListOf(1, 2, 3); mxs += 4
val m: Map<String, Int> = mapOf("a" to 1, "b" to 2)
val s: Set<String> = setOf("red", "green")
// Pair + Triple — quick tuples
val (a, b) = "Ada" to 36
// Any / Nothing / Unit
val any: Any = 1 // top type
val nothing: Nothing? = null // bottom — used for fns that never return
fun crash(): Nothing = throw IllegalStateException("unreachable")
}
Why it matters
The Kotlin standard library’s read-only collection interfaces (List, Map, Set) are views, not guarantees of immutability. For deep immutability use kotlinx.collections.immutable.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
Example
val n: Int = 7 val f: Double = 3.14 val s: String = "hi" val b: Boolean = trueTry it Yourself »
Discussion
Loading…