String is immutable in java becuase we can’t modifiy 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:

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
}
}

Output:

Prakash

Now from the above example you can see that once we create the String object we can’t modify it.

What is String?

A String is a group of Characters. String classes are used to represent these 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 of creating String objects are shown below

String S1 = new String;
String S2 = new String(“abc);
String S3 = “abc”;

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

What is the difference between creating the String object using new operator and creating the String object using double quotes?

Using new operator, we can create many objects of the same String class.

For example:

String str1 = new String(“abc”);
String str2 = new String(“xyz”);

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.

String S1 = “tutorial”
String S2 = “tutorial”

Lets see the example to demonstrate the above concepts.

class Main{
public static void main(String …a){
String S1 = “xyz”;
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 equal”);

Strint S4 = “xyz”;
if(S1==S4)
system.out.println(“s2 and s3 are equal”);
else
system.out.println(“s2 and s3 are not equal”);
}
}