Table of Contents
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,
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”;
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”);
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 myString = "Java"; myString.concat("domain");
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
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 myString = “Javadomain”;
String myString1 = myString;
myString = “Javadomain.in”;
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………