Abstract
- An object cannot be changed after being assigned a value
Eliminating Side Effects
The same set of input produces the same set of output, this helps in understanding & reasoning about code behaviour and most importantly eliminating Side Effect.
Concurrency
Immutability provides a single source of truth which helps to avoid things like Race Condition (竞态条件) in Concurrency (并发).
Then how to make changes to an object?
If we want to update an object, we need to create a new object with the changes. Thus, this allows us to make changes and ensures we don’t violet the immutability rule. However, this will incur more Main Memory usage!