What is immutable String in java

In a recent post, we have seen what is the string in java and how many ways to create a string. When we create a string by use of String class, the JVM always creates an immutable string object, it means the string is immutable in java. But what is the mean of immutable string object? How it differs from other objects? In this post we will see all the details of the java string immutable objects and how does it work in memory? After that, we will read Why string is immutable in java?

What is an immutable string?

Immutable means are unchanging over time or unable to be changed. Java defines some objects as immutable objects; developers cannot modify those objects once they are created. The meaning of immutable is unchangeable or unmodifiable. It means after the creation of the string we can’t change the value in the same object. But if you try to change in value, The JVM creates a new String object with the new value.

Whenever we create a string in java either as a string literal or by use of the new keyword. The JVM creates a string object in memory. The string object created in memory is immutable. We can’t change the value of the string in same object. If you are making any change in a string, then the compiler will create a new string instead of modification in the string.

 class ExampleStringImmutable
 {  
	public static void main(String args[])
	{  
		String studentName = "Ravi";
		studentName.concat("kant");
		System.out.println("My name is = "+ studentName);	
	}	
}  

Output: My name is = Ravi

Memory representation: In the above example, we created a string studentName with value “Ravi”.

String studentName = “Ravi”;

After the execution of the above statement, the compiler will create a string in String constant pool. String constant pool is part of heap memory.

immutable String in java

After the creation of string, we are concatenating some other string.

studentName.concat(“kant”);

After the execution of the above statement, the compiler will create a new object of string. but this object doesn’t have any reference variable.

immutable String in java

As you see in the above example the reference variable studentName is still referred to as “Ravi”. You can explicitly assign the string to the reference variable.

class ExampleStringImmutable
 {  
	public static void main(String args[])
	{  
		String studentName = "Ravi";
		studentName = studentName.concat("kant");
		System.out.println("My name is = "+ studentName);	
	}	
}  

Output: My name is = Ravikant

immutable String in java

5 thoughts on “What is immutable String in java”

  1. After reassigning the studentName variable. whatever output you printing is not correct.
    Here:
    studentName = studentName.concat(“kant”);
    System.out.println(“My name is = “+ studentName);

    Please modify the output as “Ravikant”

    Reply
  2. my question is – Now u concat the ravikanth, assign to studentName . then the value is changed now . and refer to studentName .

    Q1- how to get the old value – ravi
    Q2 – how it is achieve the it is immutable , any one can modify and refer the actual reference (studentName). when ever we wang print raviKant will display .

    Can you little clarify me .

    How
    String s=”sushil”;
    s=”Ravi”;
    s=”sushil”;
    String s1=new String (“Sushil”);
    s1=s;
    How may object will create in heap and string constant pool . Can you please clarify me memory point of view .

    Reply

Leave a Comment