liveBook · Manning

Val

As explained in Kotlin in Action, 2E

A read-only variable is a variable whose binding cannot be reassigned after initialization.

`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

How Kotlin in Action, 2E covers it

How deep it goes
Defines single assignment, type inference, conditional initialization, mutable referenced objects, and property accessors; defers deeper mutability discussion.
Examples
Uses inferred question and numeric values, conditionally assigned result, mutable languages list, and Person properties.
What it assumes
Assumes basic variable declarations while introducing Kotlin’s val and var choices from first principles.
What you can do after
Apply val to fixed local bindings and properties while recognizing that referenced objects can still be mutable.
How it connects
Connects read-only references with var, type inference, immutable objects, side-effect-free functions, properties, and Java accessors.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.