Immutability and Mutability in String

Dictionary meaning of Immutability is ” unchangeable or changeless

In Java immutability also refers to the same meaning as in dictionary but with context to String objects.

Immutable String means that any String object is non-modifiable. One cannot change String objects. If modification or any changes is done in String object, then in that case a new String object is created.

Note: Once any String object is created its data or state cannot be changed.

Let us understand the Immutable concept through the given program with diagram.

import java.lang.String;
class StringDemo { public static void main(String args[]) { String a="Ayushi"; a.concat("Academy"); System.out.println(a); } }

Output:

Let’s understand the internal concept of object creation through diagram.

From the above diagram it is now clear that two objects will be created in String Constant Pool because of two String literals as marked in the diagram. When string object (a) is modified then a new object is created in heap area but it does have any reference so it will be eligible for automatic garbage collection. When anyone tries to print the value of object a then it will print “Ayushi” because it is reference of that object.

Let’s understand immutability through another example:

import java.lang.String;
class StringDemo { public static void main(String args[]) { String a=new String("Ayushi"); a.concat("Academy"); a=a.concat("Patna"); System.out.println(a); } }

Let’s understand the above program through diagram which shows the internal memory allocation of objects on heap area as well as on String Constant pool area.

What do you understand by Mutability in String Handling

Mutability : Mutability dictionary meaning is ” changeable” or “tendency to change”.

As we have already discussed that String is immutable in nature. If we want to understand mutable feature with respect to String Handling then we have to learn StringBuffer class.

StringBuffer is a class which is found in java.lang package which shows the features of String mutability. It means whenever anyone try to change String literals in StringBuffer objects then the changes is seen in the original object. Unlike String class here no new object is created for modification or any updation in the StringBuffer class objects.

We can learn about StringBuffer class in detail in our next pages of our tutorial, but to understand mutability in brief up to this level let’s go through the given example:

class StringBufferDemo {
public static void main(String args[]) {
StringBuffer sb=new StringBuffer("Hello");
sb.append(" dear");
System.out.println(sb); }
}

Save the file as StringBufferDemo.java

Output:

Let’s understand the internal object creation in case of StringBuffer class to understand the concept of mutability.

StringBuffer Mutability Concept

It is now clear from the above diagram that when we try to change the StringBuffer class object then the changes is made done only to the original object and no new object is created as in String class.