You might have heard this before, "String is a mutable class wherein Stringbuffer is immutable". But, many of us don't know the exact difference between mutable and immutable classes.

In this post, I am trying to explain this difference.

An immutable class can not be changed over time. It means that you can not change an instance of an immutable class. However, note that a class isn't truly immutable if any of child references aren't immutable. For ex Stringbuffer

A mutable class is not immutable. You can change it, other people can change it, and you can't rely on it being the same.

Immutability is useful when designing concurrent applications because of the object state guarantees it provides. It is also good for maintaining the key of a Map.

A perfect example to demonstrate the difference between mutable and immutable class would be that of a Banking Application. In this context, a customer should always be a mutable class, as the state of a customer can change over the period of time. This applies to related objects such as a account balance, customer details, transactions etc. However, A festival discount/offer being made to a particular customer can be considered to be a immutable object, as it will remain constant over time.


Enter your email address:

Delivered by FeedBurner



Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on javagenious.com(At)gmal.com ; I would like to hear from you :)