Immutable classes - String, Integer or any other wrapper classes are immutable - meaning the object state is not changed and hashcode remains same. You can also create your own immutable class by overriding equals() and hashCode() method where in you have to return same hashCode value and marking fields as final. Immutable objects are mostly preferred in multi-threading scenarios because each thread share same piece of information and thus avoiding synchronization issues in java. Immutable objects are basically a thread-safe.
Mutable classes like StringBuffer or your own custom class where it doesn't have above all qualities. when your object state is changed your hashcode is changed. This means any thread can change your object state and thus resulting in inconsistency
No comments:
Post a Comment