In a previous article Why String is Immutable and Final in Java, I have discussed why String is immutable in nature and advantages and disadvantages String's immutability gives us.
I have also discussed that, all String literals are cached into a special memory area called String Constant Pool and how String's immutability made String constant pool possible.
But the question arises why do Java required a separate constant pool to store Strings, What's the reason, Why strings are not stored in the normal heap memory like other objects do and in this article, I will try to answer these questions.
In general, a normal Java business application deals with thousands of string objects, lots of them have same value associated and lots of them are mid operations string means they are not the final result.
So if we store all those string objects in normal heap memory, lot's of heap will be acquired by just string objects only, and the garbage collector will have to run more frequently which will decrease the performance of the application.
And that's why we have String Constant Pool and String interning process, whenever we create a string literal JVM first sees if that literal is already present in the constant pool or not and if it is there, the new variable will start pointing to the same object, this process is called String Interning.
There are two ways to create a String object
For above code there will be only one object
In above example string object with value
Ideally,
But string immutability saves us from this scenario and due to the immutability of string object string object
So String pool is only possible because of String's immutability and if String would not have been immutable, then caching string objects and reusing them would not have a possibility because any variable would have changed the value and corrupted others.
You can find complete code on this Github Repository and please feel free to provide your valuable feedback.
I have also discussed that, all String literals are cached into a special memory area called String Constant Pool and how String's immutability made String constant pool possible.
But the question arises why do Java required a separate constant pool to store Strings, What's the reason, Why strings are not stored in the normal heap memory like other objects do and in this article, I will try to answer these questions.
String Interning
Well, we know String is the most popular type present in Java and almost all Java programs use it. In fact, I have not seen a single Java program which is written without using String.In general, a normal Java business application deals with thousands of string objects, lots of them have same value associated and lots of them are mid operations string means they are not the final result.
So if we store all those string objects in normal heap memory, lot's of heap will be acquired by just string objects only, and the garbage collector will have to run more frequently which will decrease the performance of the application.
And that's why we have String Constant Pool and String interning process, whenever we create a string literal JVM first sees if that literal is already present in the constant pool or not and if it is there, the new variable will start pointing to the same object, this process is called String Interning.
There are two ways to create a String object
- Creating String Literal:: Anything which comes under
""
is a string literal e.g.String s1 = "Naresh"
, by default all string literals interned and goes to SCP.
- Creating String object using constructor: If we create a String object using the constructor e.g.
String s2 = new String("Naresh")
, the object is created in normal heap memory instead of SCP. And that's why creating String object using constructor is not considered a best practice. We can ask s2 to point to SCP instead of normal heap manually by callingintern()
method on it i.e. s2.intern().
String a = "Naresh";
String b = "Naresh";
String c = "Naresh";
For above code there will be only one object
Naresh
will be created and all reference variables a
, b
, c
will point to the same object.In above example string object with value
Naresh
will get created in SCP only once and all reference a
, b
, c
will point to same object but what if we try to make a change in a
e.g. a.replace("a", "")
.Ideally,
a
should have value Nresh
but b
, c
should remain unchanged because as an end user we are making the change in a
only. And we know a
, b
, c
all are pointing the same object so if we make a change in a
, others should also reflect the change.But string immutability saves us from this scenario and due to the immutability of string object string object
Naresh
will never change. So when we make any change in a
instead of change in string object Naresh
JVM creates a new object assign it to a
and then make the change in that object.So String pool is only possible because of String's immutability and if String would not have been immutable, then caching string objects and reusing them would not have a possibility because any variable would have changed the value and corrupted others.
You can find complete code on this Github Repository and please feel free to provide your valuable feedback.
Comments
Post a Comment