In Kotlin, when we declare a variable and assign it a value, like :
var x = 5
What happen behind the scene is, an object is created, having the value ( i.e. 5 ) you assigned to the variable ( i.e. x
). Then the variable hold the reference to the object that has been created not the object itself.
Var vs Val
The variable declare with var
are mutable, mean we can change the reference it's holding later in the code. While the variable declare with val
are immutable we can't change the reference it holding.