< Prev
Next >

String Immutability

What is String immutability?

One of the main feature of a String object is immutability. String objects are immutable i.e. once created their value cannot be changed or modified.

How is a String object immutable?

Now let's understand this with an example -:

String  s1 = "Sky";  // Statement1
String  s2 = "Blue"  // Statement2
Figure 1 below shows that after executing the two statement above, we have two String objects Sky and Blue on the Heap, which are referenced by reference variables s1 and s2 existing on the Stack.

We also know that two String object can be added using the concat() method of String class or by using + operator . So, let's try to add these two String objects.
s1 = s1 + s2; 	// Statement3

Note : Dashed-line indicates an old reference which doesn't exist anymore.

As you may see in the Figure 2, after executing the statement3, reference variable s1 points to a new String object SkyBlue, while the original String object Sky is left unmodified.

We have 3 String objects on the heap memory after executing statement3 -

This proves that String object are immutable, because modification of a String object Sky by adding another String object Blue to it, didn't modify the original String object Sky but it rather created a new String object, SkyBlue.

Are reference variables immutable too?.

As you may see in the Figure 1, reference variable s1 was initially assigned to a String object Sky but in the Figure 2, reference variable s1 is pointing to a different object SkyBlue. Hence, only a String object is immutable, while its reference variable can be modified.

String immuntability works for all the String objects.

Here in code below, we are creating 4 String objects, two of them are going to be created on String constant pool heap memory the rest two objects are going to be created on normal heap memory and we are going to prove that String immutability works for all String objects, whether they are exist on String constant pool memory or on normal heap memory.

String Immutability Example

//String Immutability 

public class String1
public static void main(String[] ar)

//String object created on String constant pool heap memory, without using new keyword
String s1= "Sky"; 
String s2= "Blue" 

s1= s1+s2; 	  //Statement 1

System.out.println("s1 points to " + s1);
System.out.println("s2 points to " + s2);

//String objects created on normal heap memory, using new keyword
String s3 = new String("New");
String s4 = new String("York");

s3= s3+s4; 	  //Statement 2

System.out.println("s3 points to " + s3);
System.out.println("s3 points to " + s4);

Output is :

s1 points to SkyBlue
s2 points to Blue
s3 points to NewYork
s4 points to York

After initially pointing to String object New, the reference variable s3 now points to a new String object NewYork. Hence, there is no reference variable pointing to String object New and it's lost. While, the reference variable s4 still points to York on normal heap memory.

Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter

< Prev
Next >
< String Constant Pool
String methods >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next