
Android Development with Kotlin
By :

In Kotlin, we have two types of variables: var
and val
. The first one, var
, is a mutable reference (read-write) that can be updated after initialization. The var
keyword is used to define a variable in Kotlin. It is equivalent to a normal (non-final) Java variable. If our variable needs to change at some point, we should declare it using the var
keyword. Let's look at an example of a variable declaration:
fun main(args: Array<String>) { var fruit: String = "orange" //1 fruit = "banana" //2 }
fruit
variable and initialize it with the vale of the orange
variable.fruit
variable with the value of the banana
variable.The second type of variable is a read-only reference. This type of variable cannot be reassigned after initialization.
The val
keyword can contain a custom getter, so technically it can return different objects on each access. In other words, we can't guarantee that the reference to the underlying object is...