MΓ³dulo 01 Β· Kotlin Basics β LecciΓ³n 1 de 5 Β· ~11 min
Constants & Variables
Naming your data
A program is mostly data with names attached. In Kotlin you attach a name to a value with one of two keywords:
valdeclares a value β a name whose reference is set once and can never be reassigned.vardeclares a variable β a name whose value you can replace later.
Kotlin strongly prefers val. A name that can't be reassigned is a name that can't drift out from under you by accident β a whole category of bugs, gone. Android Studio even warns you when you write var for something you never reassign.
Rule of thumb: start with
val. Switch tovaronly when the compiler β the program that turns your Kotlin into a runnable app β tells you the value actually needs to change.