String is immutable in java because we can’t modify or change it once it is created.
Basically Immutable itself means unmodifiable or unchangeable. In Java Once we created string object we cannot change and modify its data or state.
Below is an example to see the immutability of String in Java
[java]
class Main{
public static void main(String args[]){
String s=”Prakash”;
s.concat(” Kumar”);//concat() method appends the string at the end
System.out.println(s);//will print Sachin because strings are immutable objects
}
}
[/java]
Output:
Now from the above example you can see that once we have created the String object, we can’t modify it.
What is String?
A String is a group of Characters. In Java, String is also a class, is used to represent the groups of characters. We can even create an object of a String class without using new operator. Instead of the ‘new’ operator we use double quotes ” “.
As soon as JVM encounters ” “, it understands that it has to create an object of the String class with the contents present in ” “. And for each and every individual character inside ” “. it assigns a unique index value starting from zero.
Ex: String str = “xyz”;
Address of the object is assigned to the variable str.
This object has a very near relation with arrays. Once a String object is created, the data inside the object cannot be modified. This is the reason why we say that String is immutable.
Different ways to create String objects
[java]
String S1 = new String();
String S2 = new String(“abc”);
String S3 = “abc”;
[/java]
Consider the following two statements given below
Main m = new Main();
String str = “abc”;
Both the above statements create the objects of their respective classes.
Now, when we say System.out.println(m), it prints the address of the object whereas for System.out.println(str) it prints abc (the content of the object).
This is happening why?
Because the concept behind it is, Static Polymorphism. The println() method called in the above two cases is not the same.
One println() method is defined to accept Object of Object class and its functionality is to print the address of the object.
The second println() method is defined to accept the object of String class and its functionality is to print the contents of the object pointed by the address.
One Interesting question
Difference between creating String using new operator and creating String using double quotes?
Using new operator, we can create many objects of the same String class.
For example:
[java]
String str1 = new String(“abc”);
String str2 = new String(“xyz”);
[/java]
Here two different objects are created and the address are assigned to different variables.
But when we create objects of the String class using double quotes we can almost create only one object in the entire program. The same address of the already created object will be assigned to the variables pointing to the newly created objects.
[java]
String S1 = “tutorial”
String S2 = “tutorial”
[/java]
Below is the example to demonstrate the above concepts
[java]
class Main{
public static void main(String …a){
String S1 = “abc”;
String S2 = new String(“abc”);
String S3 = new String(“abc”);
if(S2==S3)
System.out.println(“s2 and s3 are equal”);
else
System.out.println(“s2 and s3 are not equal”);
String S4 = “abc”;
if(S1==S4)
System.out.println(“s1 and s4 are equal”);
else
System.out.println(“s2 and s3 are not equal”);
if(S1==S2)
System.out.println(“s1 and s2 are equal”);
else
System.out.println(“s2 and s2 are not equal”);
}
}
[/java]
Output