Why String in Immutable in Java?
Why String in Immutable in Java
Immutable means unchangeable or unmodifiable. Strings in Java are immutable, it means once a string is created, it cannot be modified or changed. Any change in the string leads to the creation of the new string, and the current string remains untouched. Let’s understand the immutability of Java strings with the help of the following example.
FileName: StringImmutableExample.java
public class StringImmutableExample { // main method public static void main(String argvs[]) { // Input string String str1 = "Tutorial & Example"; // Doing the concatenation work str1.concat(" – A Good Site To Learn Java."); System.out.println(str1); } }
Output:
Tutorial & Example
Explanation: The output shows that even the concatenation on the string str does not change the content of the string. When the concat() method is invoked on the string str, a new string is created. However, the new string is automatically destroyed as no reference is holding that new string. If the reference variable str gets updated and holds the reference of the new string, the old string gets destroyed. Let’s confirm it with the help of an example.
FileName: StringImmutableExample1.java
public class StringImmutableExample1 { // main method public static void main(String argvs[]) { // Input string String str1 = "Tutorial & Example"; // Updating the reference variable str1 str1 = " – A Good Site To Learn Java. "; System.out.println(str1); } }
Output:
– A Good Site To Learn Java.
Explanation: The output shows the updated string. Behind the scene, the object of the “Tutorial & Example” string gets destroyed, as we have updated the reference of the variable str1. Note that at no point of time the string “Tutorial & Example” gets manipulated.
FileName: StringImmutableExample2.java
public class StringImmutableExample2 { // main method public static void main(String argvs[]) { // Input string String str1 = "Tutorial & Example"; // Doing the concatenation work str1 = str1.concat(" – A Good Site To Learn Java."); System.out.println(str1); } }
Output:
Tutorial & Example – A Good Site To Learn Java.
Explanation: In the above code, the reference variable str is holding the reference of the concatenated string. Hence, this time the old string gets reference less and hence, destroyed.
Reason Behind Immutability
The reason behind immutability is the usage of a string literal in Java. When a string literal is assigned to a variable, the variable holds the reference of the string literal as the string literal is treated as an object in Java. One can also create another variable that holds the reference of the same string literal.
Thus, we have two reference variables holding the reference of the same string literal. Hence, if one changes the value of the string literal using one reference variable, the changes get reflected for the other reference variable too, which is bad, as the second reference variable did nothing to change the value of the string literal. Therefore, to avoid the same strings made immutable in Java.