Why String Is Immutable In Java
Well, we have mentioned earlier, and you might have heard somewhere that the strings in java, are immutable. This simply means that once the String object is created, you cannot change its content. But what if we try to change the content just by reassigning some new string to that particular object that we had created?
Why String Is Immutable In Java
We are going to observe a program now, which will give us a more clear picture, and also we are going to visualize it again as we did some time back. Have a look at the below program –
public class Stringexample {
public static void main(String[] args) {
String str1 = new String(“GyaniPandit”);
str1 = “Java”;
System.out.println(str1);
}
115
}
In the above program, firstly we created an object both in the heap and in the SCP. The str1 is referring to the object with the content ”GyaniPandit”. But the next instruction executes, which says str1 = ”Java”, and when we print str1 on the next line, we get to see Java as our output. Isn’t it strange that on one side we are saying that strings are immutable, which means once the object is created, we cannot change its content?
But here, we got the output as Java, but earlier we had set the value of the string to GyaniPandit. This is where we might get wrong. Let’s explore what the actual thing is.
Executing the first instruction, we created an object with a string literal GyaniPandit in the heap, and also a copy in the SCP because it did not exist at first. The str1 is the reference pointing to the object in the heap. So, here is a diagram for demonstrating the object creation after executing the first instruction –
So, we can clearly see that two objects are formed here, with str1 pointing to the object in the heap, which is as expected. But then comes instruction two. After executing the 2nd instruction, a new object got created in the SCP, with the string literal – Java. The str1 is now pointing to the object which contains the string literal Java. Here is the updated diagram for demonstration purposes –
The above diagram shows that the object that had been created in the heap which was earlier referred to by str1, is still there in the heap, but the str1 is now pointing to the newly created object from the SCP. The object in the heap with the string literal GyaniPandit is now eligible for something called garbage collection.
So, just looking at the output, someone can just make an assumption that the value of the string value is being changed. But this is not the thing. The object that we had created earlier (with the string GyaniPandit) is still there, with the same value. Another object was created with the string literal – Java. So, we can say that once the object is created, the value cannot be changed. This refers to immutability. If we change the value, then a new object will be created with the changed value, as seen above.
So, just remember… the contents are not changed, but a separate object(either already existing or newly created) is being referenced now.