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
- 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);
- }
- }
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
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
Now we will assign the concatenation result to s1 variable as in below program
- 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);
- }
- }
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
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.