String is Immutable, what does it mean ?

String is Immutable, what does it mean ?

String is immutable:
String objects in java is immutable. Meaning of immutable is it can not be changed or altered. In java, strings are immutable which means that string objects can not be modified or changed, instead new object will be created.

Eg:

[java]
public class Javadomain{
public static void main(String[] args) {
String myString = "Java";
myString = "domain";
System.out.println("myString is :::"+myString);
}
}
[/java]

 

Output:

 

myString is :::domain

 

 

Explanation:
Here initially we have assigned a “Java” to the string referece myString. Later we have assigned “domain” to the same string reference myString.

reference is changed, but string object did not modified, instead it created a new object for the “domain” and the myString reference changed to “domain”. This unused “Java” string object will be removed/cleared when JVM runs garbage collector.

Let’s understand this way,

string pool value assigned to reference first timenew object created instead of modying the existing ones

 

hope you understand it clearly what is immutable and what string immutable in java.

 

When you want to create the same string objects for two different references, then it will just create one and refer the same.

 

String myString = “Java”;

 

String mySecondString = “Java”;

 

 

String is immutable, what does it mean?

 

When you create the same string objects with “new” keyword, then it will create two same string objects for two different references.

 

By default it will not create two same string objects, because of new keyword it is forced to create new string objects.

 

String myString = “Java”;

 

String mySecondString = new String(“Java”);

 

String is immutable, what does it mean?

 

 

When you change the string without assigning it to the reference, then no use

 

[java]
public class StringImmutable{
public static void main(String[] args) {
String myString = "Java";
myString.concat("domain");
System.out.println(myString);
// prints Java
}
}
[/java]

 

Output:

[plain]

Java

[/plain]

 

String myString = "Java";

 

string pool value assigned to reference first time

 

String myString = "Java";
myString.concat("domain");

String is immutable, what does it mean?

 

I assigned to the reference after modifying the string object, so modifications reflecting

 

[java]

public class StringMutable {
public static void main(String[] args) {
String myString = "Java";
myString = myString.concat("domain");
System.out.println(myString);
// prints Java
}
}

[/java]

Output:

Javdomain

 

String is immutable, what does it mean?

 

Can you guess the output ?

[java]
public class StringMutable {
public static void main(String[] args) {
String myString = "Javadomain";
String myString1 = myString;
myString = "Javadomain.in";
System.out.println(myString1);
}
}
[/java]

 

Did you guess “Javadomain.in” ?

 

Then you are wrong and you should understand it better. I will help you out!

 

String myString = “Javadomain”;
String myString1 = myString;

String is immutable, what does it mean?

 

 

String myString = “Javadomain”;
String myString1 = myString;
myString = “Javadomain.in”;

 

String is immutable, what does it mean?

 

 

For an additional information I am sharing what string class implements to help you to answer in few interviews,

String class will implement comparable, Serializable and CharSequence interfaces

[java]

public final class String
implements java.io.Serializable, Comparable<String>, CharSequence
[/java]

 

 

Hope you understand what is immutable and how String works in Java. feel free to share your comments/feedbacks/suggestions………

 

Leave a Reply