Have you ever had to endure the agony of debugging a piece of code that mutates the state of an object several times throughout an application? Not only is that type of code ugly and unpredictable, it’s downright dangerous when dealing with multiple threads.
An immutable object ensures that its internal state cannot be changed after instantiation. Why does this matter? This provides an immediate benefit when working with multi-threaded code in that no synchronization needs to be performed since the object does not offer any shared, mutable state between threads. Therefore, the state of the object remains the same throughout its lifetime which makes the object automatically thread-safe.