String Immutability

One of the interesting concepts about String is its Immutability.

Yes, String is immutable in Java and what does it really means?

It means, we can’t modify the String object.

What !! then how can we use Strings in our application without modifying it ?

Sometime we need to concatenate the string and sometime we need to split the string based on requirement.

When we say, can’t modify the String object , it means we can’t modify string within the same memory.

Whenever we try to modify the content of String object then new string object will be created in a separate memory with modified content and leaving the old string object as unaffected.

This behavior is called Immutability.

Let’s see the example

  1. class ImmutableStringExample{  
  2.  public static void main(String args[]){  
  3.    String s1= new String("learning");
  4.    String s2= new String("immutability");
  5.    s1.concat(s2);
  6.    System.out.println(s1);
  7.  }  
  8. }  
class ImmutableStringExample{  
 public static void main(String args[]){  
   String s1= new String("learning"); 
   String s2= new String("immutability"); 
   s1.concat(s2);
   System.out.println(s1);
 }  
}  



Some of you question that, why output is not the concatenated string and the reason being String immutability.

Yes,when we perform concat operation, new string object gets created and old string remains unchanged and String variable “s2” is still pointing to old string only.

Let’s have a look at below diagram to understand its memory structure

String_Immutability_Befor_Concat

A reference variable “s1” is created and the string object “learning” is assigned to it.

A reference variable “s2” is created and the string object “immutability” is assigned to it.

When we call s1.concat(s2) then a new concatenated string object will be created and value of the object will be “learningimmutability”

This has not altered the “learning” object. It simply creates a new “ learningimmutability “.

But once “ learningimmutability ” is created, it is lost in our program as we are not referring to it by any variable.

So, the concatenation will happen but we are just ignoring it in our program by not assigning it to any variable.

We can see the same in below diagram


String_Immutability_after_Concat

Now we will assign the concatenation result to s1 variable as in below program

  1. class ImmutableStringExample{  
  2.  public static void main(String args[]){  
  3.    String s1= new String("learning");
  4.    String s2= new String("immutability");
  5.    s1 = s1.concat(s2);
  6.    System.out.println(s1);
  7.  }  
  8. }  
class ImmutableStringExample{  
 public static void main(String args[]){  
   String s1= new String("learning"); 
   String s2= new String("immutability"); 
   s1 = s1.concat(s2);
   System.out.println(s1);
 }  
}  




In this case,concatenated result is assigned to “s1” variable and hence output is concatenated result.

Please remember that, now string “learning” is not having any reference and hence eligible for garbage collection.

We can observe the same in below diagram


String_Immutability_after_ConcatAssign

Note :

Whenever we try to alter the existing string object then always new string object will be created and original string object remains unchanged.

This process is called as “Immutability”.

But reference variables are mutable and hence we can assign different objects to same reference variable and latest assignment will be held by the reference variable.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on