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 }
- Create a fruit variable and initialize it with the vale of the orange variable.
- Reinitialize the 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...