Prefer val over var
We'll cover the following...
When to use val
To define an immutable variable–that is, a constant or a value—use val
, like so:
val pi: Double = 3.14
Unlike Java, where you’d place the type before the name of the variable, in Kotlin you place the name of the variable first, then a colon, followed by the type. Kotlin considers the sequence Java requires as “placing the cart before the horse” and places a greater emphasis on variable names than variable types.
Since the type of the variable is obvious in this context, we may omit the type specification and ask Kotlin to use type inference:
...