Understanding the immutability of variables

Typically, like all other programming languages, the values of variables can be changed. The following snippet shows an example:

var variable = 5

variable = 6 //You can change value

However, if val (instead of var) is used to define a variable, then the variable is immutable. The value of the variable cannot be changed. This is similar to final variables in Java. Consider the following code:

val immutable = 6

//immutable = 7 //Val cannot be reassigned
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset