What is the method for defining variables in Kotlin?
In Kotlin, variables can be defined using the keywords var or val.
- Variables defined using the var keyword are mutable, meaning they can be reassigned, similar to regular variables in Java.
Example: var x: Int = 5 - Variables defined using the ‘val’ keyword are immutable; once assigned a value, they cannot be changed, similar to constants in Java.
Example: val y: String = “Hello”
In the above example, x is a mutable integer variable with an initial value of 5 that can be changed through assignment operations. On the other hand, y is an immutable string variable with an initial value of “Hello” that cannot be modified.