🐦 Kotlin Beginner

What is the difference between val and var in Kotlin?

Answer

In Kotlin, val declares an immutable (read-only) reference — once assigned, the reference cannot be changed. It is equivalent to Java's final. var declares a mutable variable that can be reassigned any number of times. Best practice is to always prefer val by default and only use var when mutability is truly needed. Note that val means the reference is immutable, not the object it points to — a val list can still have its contents modified.