November 2018
Intermediate to advanced
388 pages
9h 5m
English
Immutability is the opposite of mutation. Mutation is where we modify the state of an object. Mutation is error-prone and makes debugging difficult. Kotlin emphasizes immutability with val, which makes a variable immutable. Its collection types are also immutable by default. Once the val or collection is initialized, it is guaranteed to hold the same value. Immutability makes code easier to write and use and the internal state of the application will be more consistent.
Let's consider the following code sample:
object ImmutabilityTest { @JvmStatic fun main(args: Array<String>) { val msg = "Hello" msg = "hi" println(msg) }}
In the preceding code, we created a immutable variable, msg, assigned a value. We then try to change its ...
Read now
Unlock full access