
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Mutable and Immutable Object
In Java, state of the immutable object can't be modified after it is created but definitely reference other objects. They are very useful in multi-threading environment because multiple threads can't change the state of the object so immutable objects are thread-safe. Immutable objects are very helpful to avoid temporal coupling and always have failure atomicity and also helpful in multiple threading. Why though? because no one can change the object right? So, it becomes thread-safe, it means it will not cause any unexpected issues when different parts of the program are trying to access that particular object.
On the other hand, we have mutable objects, which we can change at any state. Their fields can be changed after they are created. But because of that, they are not thread-safe, and we will need to be extra careful when we work with them in a multi-threaded program.
Now, let us see in details what are the differences between these two.
Mutable V/S Immutable Objects
Key | Mutable Object | Immutable Object |
---|---|---|
Basic Idea | We can modify the state of a mutable object after it is created. For example, if we create an object called laptop with a brand name (like Dell) and later on update the name of the brand (like HP). | Once you create an immutable object, its state gets locked in and we cannot change it. If we need to set something different later on, instead of changing the particular field we will need to create a new object. |
Thread Safety | Mutable objects are not thread-safe, meaning if multiple threads try to access or modify the object at the same time, it can cause issues, so we need to take care of it beforehand. | Immutable objects are thread-safe. Because we can't change their state after creation, different threads can access the object without any kind of interference or unexpected behavior. |
Final Class | A mutable class doesn't need to be a final class. It can be extended through other subclasses, and its behavior can be modified through subclasses. | To create an immutable object, make the class final. This prevents anyone from creating subclasses that could modify the behavior of the class and make it mutable. |
Examples | By default all classes and their objects are mutable by nature. Classes like ArrayList and HashMap allow their contents to be changed after creation, making them mutable by nature. | In Java, classes like String and wrapper classes such as Integer and Boolean are examples of immutable classes. Once they are created, their state cannot be modified later. |
Example of an Immutable Class
public final class ImmutableClass { private final String laptop; public ImmutableClass(String laptop) { this.laptop = laptop; } public String getLaptop() { return laptop; } } public class Main { public static void main(String[] args) { ImmutableClass immutableClass = new ImmutableClass("Dell"); System.out.println(immutableClass.getLaptop()); } }
Output
Dell
Example of a Mutable Class
public class MutableClass { private String laptop; public MutableClass(String laptop) { this.laptop = laptop; } public String getLaptop() { return laptop; } public void setLaptop(String laptop) { this.laptop = laptop; } } public class Main { public static void main(String[] args) { MutableClass mutableClass = new MutableClass("Dell"); System.out.println(mutableClass.getLaptop()); mutableClass.setLaptop("IBM"); System.out.println(mutableClass.getLaptop()); } }
Output
Dell IBM