Javatpoint Logo
Javatpoint Logo

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:

  • Methods for modifying the field values
  • Getters and Setters of the objects

Consider the below example of the mutable class:

Output:

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:

  • Final class, which is declared as final so that it can't be extended.
  • All fields should be private so that direct access to the fields is blocked.
  • No Setters
  • All mutable fields should be as final so that they can not be iterated once initialized.

Consider the below example:

Output:

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:

  • The mutable objects can be changed to any value or state without adding a new object. Whereas, the immutable objects can not be changed to its value or state once it is created. In the case of immutable objects, whenever we change the state of the object, a new object will be created.
  • Mutable objects provide a method to change the content of the object. Comparatively, the immutable objects do not provide any method to change the values.
  • The mutable objects support the setters and getters both. Comparatively, the immutable objects support only setters, not getters.
  • The Mutable objects are may or may not be thread-safe, but the immutable objects are thread-safe by default.
  • The mutable class examples are StringBuffer, Java.util.Date, StringBuilder, etc. Whereas the immutable objects are legacy classes, wrapper classes, String class, etc.

Consider the below table:

Mutable Immutable
We can change the value of mutable objects after initialization. Once an immutable object is initiated; We can not change its values.
The state can be changed. The state can not be changed.
In mutable objects, no new objects are formed. In immutable objects, a new object is formed when the value of the object is altered.
It provides methods to change the object. It does not provide any method to change the object value.
It supports get() and set() methods to dela with the object. It only supports get() method to pass the value of the object.
Mutable classes are may or may not be thread-safe. Immutable classes are thread-safe.
The essentials for creating a mutable class are methods for modifying fields, getters and setters. The essentials for creating an immutable class are final class, private fields, final mutable objects.

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.

Conclusion

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.


Next TopicPOJO




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA