`val` declares a read-only reference in Kotlin: its binding is assigned once and cannot later be reassigned. Prefer `val` by default, changing to `var` only when the binding genuinely needs a new value. This makes mutability explicit and supports code built from read-only references, immutable objects, and functions without side effects. section 2.1.5 section 1.4.3
Declare and infer a read-only value
```kotlin val question = "The Ultimate Question of Life, the Universe, and Everything" val answer = 42 val yearsToCompute = 7.5e6 ``` The compiler infers each type from its initializer: `answer` is an `Int`, while a floating-point constant produces a `Double`. An explicit type remains available when it improves clarity or is required because initialization happens later. section 2.1.4
A `val` must be initialized exactly once during execution of its block. It may be declared before its value is known, provided the compiler can prove that exactly one assignment occurs, such as mutually exclusive `if` branches. section 2.1.5
fun canPerformOperation(): Boolean {
return true
}
fun main() {
val result: String
if (canPerformOperation()) {
result = "Success"
} else {
result = "Can't perform operation"
}
}
`result` is declared once, then assigned in either branch. Because only one branch can run, it remains a valid `val` rather than requiring `var`. section 2.1.5
Read-only applies to the reference, not necessarily to the object it names. A `val` holding a mutable list cannot point to another list, but the existing list may still be changed. Do not equate `val` with an immutable object. section 2.1.5
Use val for a property
```kotlin class Person( val name: String, var isStudent: Boolean ) ``` A `val` property is read only and has a getter; a `var` property also has a setter. Kotlin properties replace the usual Java combination of field and accessor methods. A Kotlin `val name` is exposed to Java through `getName()`. section 2.2.1
`val` is especially useful in compact Kotlin code that builds values from expressions: a list, a computed oldest person, or a nullable value handled with `?:`. It keeps the name fixed while allowing the program to express how the value was obtained. section 1.1
