➤ Immutable Class in Java
➤ How to create immutable class
➤ Why String is Immutable in Java
Mutable vs Immutable in Java. An object which allows modification on the current object is called a mutable object. An object which doesn’t allow modification on its data or if it allows modification then the modified result is stored in the different new object, not in the current object’s memory is called an immutable object.
The example of mutable objects is StringBuffer and StringBuilder class objects. The modified string data is stored in the same object for these classes.
The example of immutable objects is Wrapper classes and String class. The wrapper class doesn’t allow any modifications. String class allows modification but it store the modified result in the new String object, not in the same String object.
In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created.
Mutable Objects Example
public class Test1 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Know");
System.out.println(System.identityHashCode(sb));
System.out.println(sb);
// Modify the object
sb.append(" Program");
System.out.println(System.identityHashCode(sb));
System.out.println(sb);
}
}
The output of the above program:-
517938326
Know
517938326
Know Program
In this program, we have created one StringBuffer object with a string literal “Know”. Then we want to modify its content.
Since the StringBuffer class is a mutable class, therefore objects of the StringBuffer class allow modification and store results to the same objects. Therefore after calling the append() method on the current StringBuffer object its content becomes “Know Program”. Generated result is stored in the same object. Due to this reason, we are getting similar hashcode values.
Immutable Objects Example
public class Test2 {
public static void main(String[] args) {
String str = new String("Know");
System.out.println(System.identityHashCode(str));
System.out.println(str);
// Modify the object
str = str.concat(" Program");
System.out.println(System.identityHashCode(str));
System.out.println(str);
}
}
The output of the above program:-
358699161
Know
2143192188
Know Program
In this program, we are creating a string object with the string literal “Know”. Since String class is an immutable class and doesn’t allow modification on the same object. Therefore after each modification, if the result value is different from the current object then another string object will be created.
After calling concat() method on the String object, the result will be “Know Program” which is different from the current value “Know”. Therefore, another string object is created, the result stored in that object, and assigned to the referenced variable “sb”. The existing string object is eligible for garbage collection. Both string objects are different therefore we are getting different hashcode values for these strings.
Mutable vs Immutable in Java
| Mutable | Immutable |
|---|---|
| An object which allows modification on the current object is called a mutable object. | An object which doesn’t allow modification on its data or if it allows modification then the modified result is stored in the different new object, not in the current object’s memory is called an immutable object. |
| A class, whose objects are mutable in nature is called an immutable class. | A class, whose objects are immutable in nature is called an immutable class. |
| Example of Mutable class:- StringBuffer, StringBuilder, and e.t.c. | Example of Immutable classes:- String, Wrapper classes of primitive types, File class, and e.t.c. |
| Mutable classes may or may not be thread-safe. | Immutable classes are thread-safe. |
| After any modification on the current object, the same object will be used to store the data rather than creating a new object. | After each modification, if the result is different from the current object value then a new object will e created. |
| 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. |
Note:- Generally a class is mutable unless special effort is made to make it immutable. Most of the Java classes are mutable classes. By default, all the Java classes created by us will be mutable classes unless we add immutable behavior to them.
There are two types of immutable classes in Java:-
1) Immutable classes which allow modification. In these classes, after modification, if the content is different then a new object will be created. Else the same object will be reused. Example:- String class.
2) Immutable classes which don’t allow modification. Example:- All Wrapper classes for the primitive types, and java.io.File class. These classes don’t provide any method to perform modification operations.
Recommended articles:-
- What is Mutable in Java?
- Immutable class in Java.
- Why string is Immutable in java?
- How to create immutable class in Java?
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!