val vs var
In this page:
val name = value // read-only reference
var name = value // can be reassigned
name = newValue // only allowed for var
Declaring with val
val creates a reference that can be assigned a value exactly once. Attempting to assign a new value to a val after that is a compile-time error, which helps prevent accidental changes.
val everywhere possible; it makes code easier to reason about.Example: Declaring with val
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `name`, set to `"Kotlin"`
val name = "Kotlin"
// Print "Language: $name" to the console, with a trailing newline
println("Language: $name")
}
Login to try C/C++/Java/PHP code in the editor
Declaring with var
var creates a mutable reference whose value can be changed later using =. It is used when a value genuinely needs to change over the program's lifetime, such as a running counter.
Example: Declaring with var
// Entry point: execution of the program starts here
fun main() {
// Declare a mutable variable `counter`, initialized to `0`
var counter = 0
counter = counter + 1
counter += 1
// Print "Counter is now $counter" to the console, with a trailing newline
println("Counter is now $counter")
}
Login to try C/C++/Java/PHP code in the editor
val Reference vs Mutable Contents
A val only locks the reference itself, not necessarily what it points to. A val holding a mutable list still lets you add or remove items -- you just cannot point that val at a different list afterward.
Example: val Reference vs Mutable Contents
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `items`, set to `mutableListOf("apple", "banana")`
val items = mutableListOf("apple", "banana")
// Add `"cherry"` to `items`
items.add("cherry")
// Print `items` to the console, with a trailing newline
println(items)
}
Login to try C/C++/Java/PHP code in the editor
Choosing val Over var
Idiomatic Kotlin favors val by default and reaches for var only when a variable must be reassigned. This habit reduces bugs caused by unexpected mutation and makes code easier to follow.
Example: Choosing val Over var
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `pi`, set to `3.14159`
val pi = 3.14159
// Declare a mutable variable `radius`, initialized to `2.0`
var radius = 2.0
radius = 3.0
// Print "Area: ${pi * radius * radius}" to the console, with a trailing newline
println("Area: ${pi * radius * radius}")
}
Login to try C/C++/Java/PHP code in the editor
- Trying to reassign a
valand being surprised by a compiler error;valmeans read-only, not just a stylistic default. - Overusing
varout of habit from other languages, when most values in Kotlin never actually need to change. - Thinking
valmakes an object itself immutable; it only prevents reassigning the variable, the object's own contents can still change.
valdeclares a read-only reference; it can be assigned once and never reassigned.vardeclares a mutable reference that can be reassigned as many times as needed.- Preferring
valby default is idiomatic Kotlin; usevaronly when reassignment is truly needed. valdoes not make the referenced object immutable -- aval listcan still have items added to it.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: