6 Variables and properties
24
An immutable object is an object whose state cannot change after instantiation. If you need a
modified version of the object, a new object needs to be created. This makes programming much
more robust and predictable. In Java, most objects are mutable, which means that any part of the
code which has access to the object can modify it, affecting the rest of the application.
Immutable objects are also thread-safe by definition. As they can’t change, no special access control
must be defined, because all threads will always get the same object.
So the way we think about coding changes a bit in Kotlin if we want to make use of immutability.
Do'stlaringiz bilan baham: