What do you understand by Immutablity of strings?

In Java strings are immutable. Dictionary meaning of immutable is a thing which cannot be changed. In Java, we cannot change the contents of a string once we have created it. Whenever we try to change the contents of a string a new String object is created with changed contents instead of modifying the original one.Thus the original string remains intact. For example,

String s="James";
System.out.println(s) // will print James, Not James Gosling.

concat () method of String class appends "Gosling " at the end of "James". So the new string will become "James Gosling". But if you see the output of this program  it will print James, NOT James Gosling.

This is because when you append Gosling to original String s, a new String object with the contents James Gosling created but the original string s is not modified. So now there are two string objects - one with contents James (original one ) and one with changed contents James Gosling (new one).

Variable s is still pointing to original string object while we have not stored the reference of new string any where. Therefore when we print value of s, it prints James instead of James Gosling.

Suppose you change above statements to:

String s="James";
System.out.println(s);    //Now it will print James Gosling, Not James.

Now the output will be James Gosling.

This is because concat() returns the reference to new String object with changed contents. You have stored this reference in variable s which previously was pointing to original string. Now s is pointing to changed string (new one) and reference to previous original string got lost.

Example 1:

String s= "Marie";
String s1=s.concat(" Curie");
System.out.println(s)      // will print Marie
System.out.println(s1);     // will print Marie Curie
System.out.println(s==s1); //will print false i.e two different objects

Last statement will print false. This shows that s and s1 are pointing to two different strings objects.

Example 2:

String first="Eclipse";
String sec="is a good IDE";
String third=first+sec;
System.out.println(first);    // will print Eclipse
System.out.println(sec);      // will print is a good IDE
System.out.println(third);   // will print Eclipse is a good IDE

Here first, sec, third are all pointing to different strings.

Here we have used string concatenation operator (+). Upon concatenating two strings - first and sec, + returns a new String object with concatenated contents. Reference of this new object is stored in variable third. Thus there are now three String objects-first, sec and third.

Example 3:

This example is as same as example 1 but here we have used substring() method of String class instead of concat(). substring() also returns a new String object instead of modifying the original string.

String s= "James Gosling";
System.out.println(s); // will print James Gosling, NOT Gosling

Here original string s remains intact.

If you change code to:

String s= "James Gosling";
System.out.println(s);  // Now it will print Gosling

Example 4:

String s="NetBeans is also a good IDE";
System.out.println(s);    // will print NetBeans is also a good IDE

If code is changed to:

String s="NetBeans is also a good IDE";
System.out.println(s); // will print NETBEANS IS ALSO A GOOD IDE.

I would like to know your comments and if you liked the article then please share it on social networking buttons.