Scientech Easy Immutable String in Java with Example Program | Scientech Easy

Tuesday, April 9, 2019

Immutable String in Java with Example Program

Why String is Immutable in Java?


String class in Java is immutable. The meaning of immutable is unchangeable or unmodifiable. i.e, Once we create a string object with value, you are not allowed to perform any changes in that object. In other words, you cannot modify the value of the string. But if you try to change with a new value, a new string object will be created by storing a new value. 
So, we cannot perform any changes with the existing string object. This non-changeable behavior is nothing but an immutability concept in Java.
Java implements this immutability concept to minimize the duplication of string values that tend to exist many times in any application program.

Let's understand the immutability concept by taking an example program.
Program source code 1:
    package stringPrograms; public class ImmutabilityTest { public static void main(String[] args) { String s="hello"; s.concat("world"); // concat() method adds the string at the end. System.out.println(s); // It will print "hello" because strings are immutable objects. } }
    Output: hello
Now let's understand the output of the above program by the given below figure and its explanation.
Immutable string
Explanation:
When JVM will execute this statement String s="hello";, it will create a string object in the string constant pool and stores "hello" in it. 
When the statement s.concat("world"); will be executed by JVM, it will create two new objects because we are trying to modify the original content.

1. First, for every string literal "world", JVM will create one copy of string object in the string constant pool.
2. The second object will be created in the heap with modified content "hello world". Since string concatenation is executed at the runtime. Therefore, if a new object is required to create, this new object is always created in the heap area only, not in string constant pool.
Since this new object is not assigning with any reference variable, therefore, it is called unreferenced object and the garbage collector will automatically remove it from the memory.
Thus, the value of string s is not modified and still, 's' is pointing to the "hello" only. Therefore, the result is "hello". This is the reason, string objects are called immutable in Java.
Key points: 
String concatenation cannot be resolved at the compile-time. It is always executed at runtime. This causes an extra object to be generated.

Why String objects are immutable in Java?


String objects are immutable in Java because Java uses the concept of string constant pool. Suppose there are 6 reference variables, and pointing to the same object "Hello world". If one reference variable of them changes the value of the object from "Hello world" to "Hello", with this change, all the reference variable will be affected. That's why string objects are immutable in Java.
The main advantage of string immutability is that Java compiler can save space in the memory by sharing strings.

Final words 
Hope that you will have understood the concept of Immutable string in Java with an example program. I hope that you will have enjoyed this article. If you have any problem to understand this topic then you start from the basic of strings.
Thanks for reading!