I have read a lot of places where it is written that immutability in java is a desired feature. Why is this so?
-
2[This answer](http://stackoverflow.com/questions/3407403/whats-the-advantage-of-a-string-being-immutable?answertab=votes#tab-top) may interest you – Reimeus Mar 10 '13 at 03:08
-
Think "immutable" == "thread safe". – duffymo Mar 10 '13 at 03:10
-
1A rock is also thread safe. The purpose has very little to do with thread safety. – Hot Licks Mar 10 '13 at 03:13
-
There may be other duplicates to this question, but there are issues which are applicable to classes in general which go beyond those issues that are only relevant for strings. – supercat Mar 12 '13 at 16:55
5 Answers
Immutability makes it easier to reason about the life cycle of the object. It is especially useful in multi-threaded programs as it makes it simpler to share between threads.
Some data structures assume the keys or elements are immutable, or are not changed in critical ways. e.g. Maps and Sets. They don't have to be strictly immutable but it makes it a lot easier if they are.
The downside of immutable objects is that it make recycling them much harder and can significantly impact performance.
In short, if performance is an issue, consider mutable objects, if not use immutable objects as much as possible.

- 525,659
- 79
- 751
- 1,130
-
1
-
-
It's not a perfect answer, but it does annoy me if people believe an answer is wrong, but make no attempt to get it corrected, clarified or improved. The whole purpose of this site is to learn something. – Peter Lawrey Mar 10 '13 at 03:18
-
-
Even a right answer which is difficult to understand can be improved. – Peter Lawrey Mar 10 '13 at 03:20
-
Agree with Mik378 - sometimes I feel that the SA community has become a bit hostile than what it used to be. – Manidip Sengupta Mar 10 '13 at 03:21
-
2I prefer to give constructive criticism than down vote. The only time I just down vote is if I agree with comments already made. – Peter Lawrey Mar 10 '13 at 03:26
-
1@PeterLawrey: I wouldn't say that immutability makes it easier to reason about the life-cycle of the object, but rather that in many cases it makes the life cycle *irrelevant*. Garbage-collection is far more useful with immutable objects than with mutable ones (indeed, if I were designing a small embedded framework, I'd probably use Java-style GC for immutable objects and Objective-C-style ref-counting for mutable ones). In many cases, a mutable objects should have one well-defined owner, and useful life-time is tied to that owner. Immutable objects have no owner; they just exist. – supercat Mar 12 '13 at 16:53
-
The main benefit of immutability is that an object cannot be changed "out from under you". Eg, consider a String that is a key to a Map. If it were mutable then one could insert a key/value pair then modify the String that is the key. This would break the hash scheme used in the map, and lead to some potentially very unpredictable operation.
In some cases such mutability could lead to security exposures, due to intentional abuse, but more commonly it would simply lead to mysterious bugs. And "defensive" copying of the mutable objects (to avoid such bugs) would lead to many more objects being created (especially if a formulaic approach to the problem is taken).
(On the other hand, lots of objects are created, eg, because a new String is created when someone appends to the String, or "chops off" a part of it, vs simply mutating the existing String, so both approaches end up causing more objects to be created, for different reasons.)

- 47,103
- 17
- 93
- 151
Immutability is highly desired when you need to know that a class hasn't changed when you don't expect. Consider the keys in a hash map. If you set a mutable object for a key, then store it at the key's current hash value (mod hash array size), what happens when you change it? The hash changes and suddenly it's in the wrong spot! You (the HashMap) don't know that it has changed, so you don't know to update its location, so the next time someone tries to look up that key, you won't find it. Immutability removes this problem - if you put an object somewhere based on its hash, you know it will always be where you expect it to be.
And, as pointed out elsewhere, being immutable means it's safe to read concurrently from multiple threads without synchronization.

- 53,822
- 15
- 101
- 132
In programming, an immutable class or object is an object whose state can not be modified after it is created. Some cases they are still considered as immutable even if you can change their attributes (fields), but the object nature keeps same.
Immutable Objects are often desired because 3 main reasons:
- Thread-safe
- Higher security than mutable objects
- Simplicity
There is a ref. that you can review for a better understanding about immutability for java classes and objects

- 1,586
- 3
- 27
- 43
Effective Java Item 15 Immutable classes are easier to design, implement, and use than mutable classes. They are less prone to error and are more secure.

- 133,369
- 30
- 199
- 275