Mutable and Immutable in Java
Java is an object-oriented programming language. As it is an object-oriented programming language, it's all methods and mechanism revolves around the objects. One object-based concept is mutable and immutable in Java. Objects in Java are either mutable or immutable; it depends on how the object can be iterated.
In this section, we will discuss mutable and immutable objects in Java. Further, we will see the difference between them.
What are Mutable Objects
The mutable objects are objects whose value can be changed after initialization. We can change the object's values, such as field and states, after the object is created. For example, Java.util.Date, StringBuilder, StringBuffer, etc.
When we made a change in existing mutable objects, no new object will be created; instead, it will alter the value of the existing object. These object's classes provide methods to make changes in it.
The Getters and Setters ( get() and set() methods ) are available in mutable objects. The Mutable object may or may not be thread-safe.
What are Immutable Objects
The immutable objects are objects whose value can not be changed after initialization. We can not change anything once the object is created. For example, primitive objects such as int, long, float, double, all legacy classes, Wrapper class, String class, etc.
In a nutshell, immutable means unmodified or unchangeable. Once the immutable objects are created, its object values and state can not be changed.
Only Getters ( get() method) are available not Setters ( set() method) for immutable objects.
Let's see how to create classes for mutable and immutable objects.
How to Create a Mutable Class
The following two things are essential for creating a mutable class:
Consider the below example of the mutable class:
JavaTpoint Java Training
From the above example, we are changing the name value using the setName method.
How to Create an Immutable Class
The following things are essential for creating an immutable class:
Consider the below example:
Core Java Training
Hence, we have discussed the mutable and immutable objects and classes. Let's discuss the differences between them:
Difference between Mutable and Immutable Objects
The following are some key difference between mutable and immutable objects in Java:
Consider the below table:
Why are Strings in Java Immutable
String in Java is a very special class, as it is used almost in every Java program. That's why it is Immutable to enhance performance and security. Let's understand it in detail:
In Java, strings use the concept of literals. Suppose we have an object having many reference variables. In such a scenario, if we will change the value of a reference variable, it will affect the entire object and all of its values.
Apart from the above reasons, the following reasons are also responsible for making the String immutable:
Immutable objects are very simple; there is no need for synchronization and are inherently thread-safe. But, Immutable objects make good building blocks for other objects, so we have to provide them special care.
Most Developers make the String the final object so that it can not be altered later.
We have discussed mutable and immutable objects in Java, now, we have a clear understanding of both mutable and immutable objects and classes. Further, we have seen how to create a mutable and immutable class.
It is noted that immutable objects can be changed to their value and state once initiated and may or may not be thread-safe. Comparatively, the immutable objects can not be changed to their state and value and default thread-safe.