February 2018
Intermediate to advanced
350 pages
7h 35m
English
So, in order to encourage immutability but still let the developers have the choice, Kotlin introduced two types of variables. The first one is var, which is just a simple variable, just like in any imperative language. On the other hand, val brings us a bit closer to immutability; again, it doesn't guarantee immutability. So, what exactly does the val variable provide us? It enforces read-only, you cannot write into a val variable after initialization. So, if you use a val variable without a custom getter, you can achieve referential immutability.
Let's have a look; the following program will not compile:
fun main(args: Array<String>) {
val x:String = "Kotlin"
x+="Immutable"//(1)
}
As I mentioned earlier, ...
Read now
Unlock full access