Immutability in java
In Java, Object can be of two types :- Mutable Immutable Mutable objects are the ones whose state can be changes during its lifetime. This is in contrast with Immutable objects , whose state cannot be changed once created. We have many classes in Java which provide immutable objects like String, Integer etc. In this post, I will discuss the immutability in detail, with its features and where can we use this. In basic terms, Immutable objects are the ones, whose state cannot be changed.e.g Integer i = new Integer(10) ; The Integer class provides us no method which can change the state/value of i. So we say Integer is an immutable class & object i is an immutable object. We have many benefits for it: - They are good candidates to cache :- Since immutable objects do not change their state, we can share or cache them and retrieve them in future, without copying and cloning the object. Solid keys :- Immutable objects makes the best keys for Hash classes. HashCode() ...