What is Mutable in Java

What is mutable in Java? Once we create an object and if it allow to perform modification, and after modification, if it store results on the same object then this nature is called mutable.

What is a mutable object in Java? An object which allows modification on the current object is called a mutable object. The classes whose objects are mutable are called mutable classes. The example of mutable classes is StringBuffer and StringBuilder. In these classes, modified string data will be stored in the same object.

Mutable class Example

public class Example {
   public static void main(String[] args) {
      StringBuilder sb = new StringBuilder("Learn Java"); 
      System.out.println(System.identityHashCode(sb));
      System.out.println(sb);
      
      // modification
      sb.append(" - Know Program");
      System.out.println(System.identityHashCode(sb));
      System.out.println(sb);
   }
}

The output of the above program:-

358699161
Learn Java
358699161
Learn Java – Know Program

Since the StringBuilder class is a mutable class therefore it allows modification on the object and after every modification, it store the result in the same object.

In the above program, we created a StringBuilder object with the content “Learn Java” and assigned it to the reference variable “sb”. Then we added string literal ” – Know Program” to the current object. After modification, the generated new data is “Learn Java – Know Program” which is stored in the same object. Due to this reason, we are getting the same hashcode values.

The opposite of mutable behavior is immutable. 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. See more here:- Immutable class in Java.

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.

Immutable Class Example

public class Example2 {
   public static void main(String[] args) {
      String str = new String("Learn Java"); 
      System.out.println(System.identityHashCode(str));
      System.out.println(str);
      
      // modification
      str = str.concat(" - Know Program");
      System.out.println(System.identityHashCode(str));
      System.out.println(str);
   }
}

The output of the above program:-

358699161
Learn Java
2143192188
Learn Java – Know Program

Since the String class is an immutable class, therefore it doesn’t allow modification on the same object. After modification, if the result is different from the current object value then it will create another string object else it will use the existing object.

In this program, we have created one string literal “Learn program” using which one string object is created and assigned to the reference variable “str”. Then to perform modification we are calling concat() method of the String class. The generated result “Learn Java – Know Program” is different from the current value “Lean Program” therefore another string object is created. The current object will be eligible for garbage collection. Therefore, we are getting different hashcode values.

Recommended articles:-

  • Immutable class in Java.
  • Mutable vs immutable.
  • 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!

Leave a Comment

Your email address will not be published. Required fields are marked *