An immutable value is one whose content cannot be changed without creating an entirely new value.
It can be beneficial to use immutable objects for several reasons:
- To improve performance (no planning for the object's future changes)
- To reduce memory use (make object references instead of cloning the whole object)
- Thread-safety (multiple threads can reference the same object without interfering with one other)
- Lower developer mental burden (the object's state won't change and its behavior is always consistent)
Note that you can easily prove mutability: an object is mutable as long as it offers one way to change its properties. On the other hand, immutability is hard to prove if there are no language semantics to secure it — it is a matter of developer contract. For example,