Concept of String Pool And String Object Creation In Java
A detailed explanation of how many objects are created while using “string literals” vs using the “new” keyword and JVM allocates memory when it comes to the String class.
Hello! Welcome to a new blog. Today I am going to talk about a very common yet very important concept. In interviews, you will get a lot of questions centered around this topic.
Why is String immutable? What is String constant pool? How many objects are created when you write this piece of code
String s1 = new String("Guess");
? What are the benefits of having a String Pool? What doesString.intern()
method do? etc.
Let’s find the answers to all of the questions by exploring what happens behind the scenes when you create a String Object using different approaches
What is String Pool in Java?
String Pool is a place in the HEAP memory of Java to store string literal.
String allocation, like all object allocation, proves to be a costly affair in both the cases of time and memory. The JVM performs some steps while initializing string literals to increase performance and decrease memory overhead. To decrease the number of String objects created in the JVM, the String class keeps a pool of strings.
Each time a string literal is created, the JVM checks the string literal pool first. If the string already exists in the string pool, a reference to the pooled instance returns. If the string does not exist in the pool, a new String object initializes and is placed in the pool.
String Pool is possible only because String is immutable in Java and its implementation of String interning concept.
String Immutability and Advantages
If you look at the String class definition, it has been declared as final. So once the String object is created, it cannot be modified.
Strings are immutable in java because String objects are cached in String Pool. Since cached String literals are shared between multiple objects, so there is always a risk, where changes made by an object affect others.
Advantages
- Strings are very popular candidates for the HashMap key, they need to be immutable to retrieve the value object stored in HashMap. Since HashMap works in the principle of hashing, which requires the same has value to function properly. Since String is immutable, its hashcode is cached at the time of creation and doesn’t need to be calculated again. If Strings were mutable, it would produce two different hashcodes at the time of insertion and retrieval if contents of String were modified after insertion, which is not at all ideal.
- Another reason is, it helps JVM save a lot of heap space, by caching the String literals in the pool.
- In Java, security parameters are typically represented as String, for example, in-network connections, database connection URLs, usernames/passwords, etc. If it were mutable, these parameters could be easily changed. It’s also used extensively by JVM class loaders while loading classes.
- Making String immutable automatically makes them thread-safe, thereby solving synchronization issues.
Enough of theory! Let’s see some code snippets and understand the above concepts.
Basic Concept With An Example
String objects are created in two ways :
1. Using double quotes(“ ”) [when String literals are used ]
Whenever we use this approach to create Strig objects it makes use of the string pool concept and thus is efficient.
String s1= “Arpan”;
The above statement first searches for the string “Arpan” in the string pool
, if found it just gives it a reference s1
. If not found it creates a string object and places it in the string pool
and then gives it a reference s1.
In this case, only one object is created: in the
string pool
.
2. Using the ‘new’ keyword :
String s2= new String(“Banerjee”);
The above statement creates a string object in heap memory
and checks whether it is present in the string pool
or not. If the string “Banerjee” is not present in the string pool
then it will place this string in the string pool
else it will skip it.
Case 1: Two objects are created: one in the
heap memory
and the other in thestring pool
.Case 2: Only one object is created: in the
heap memory
.
Now you might have this question
Let’s say we create a String using the above approach. So, a string object will be created In the heap memory having the value “Banerjee” and s1
will point to it. Now, if “Banerjee” is not present in the string pool, I just said that it will create an object in the string pool as well.
But, have you ever thought of this, who will point to this object? s1
?
No,
JVM
will point to that object.
Therefore, in the interest of speed and optimization, it is always advisable to use string literals way of creating strings, unless we specifically want a new object to be created for the string.
Understanding Immutability With An Example
Let’s create a string with string literals notation.
String s1= "java";
This, as usual, creates a string containing "java"
and assigns it a reference s1
. Simple enough? Okay.
Let’s see what happens when we modify the string:
s1= s1.concat(" code");
This appends a string " code"
to s1
. Now what do you get when you try to print the value of s1
.
System.out.print(s1);
-> java code
But, we just learned that strings are immutable, right? But the console says something else, is it?
Well no, the above operation did not modify s1
. JVM
created a new String
object gave it a value "java code"
, and gave it a reference str
.
Here we have 3 objects:
java
,code
andjava code
.s1
points tojava code
.
A code sample to make the concepts more clear.
So, can you tell me how many objects are created in the above program? Think about whatever we learned till now….
4 objects will be created in the above program.
- Two in the
heap
: “Arpan” pointed byst1
and “Banerjee” byst2
, because of thenew
keywords. - Two in the
string pool
: “Banerjee” pointed byJVM
and “Arpan” pointed bys1
,s2
ands3
.
Here is the diagram of the objects and memory allocation:
Advanced examples
How many objects are created in the above example?
6 objects are created. By now you must know why and how.
See the diagram below if you have any confusion.
One Last Thing: String.intern() Method
The intern() method of the String method returns a canonical representation for the string object.
It creates an exact copy of a String object in the heap memory and stores it in the String constant pool.
When the intern method is invoked, if the string pool already contains a string equal to this newly created String object using String.intern()
method, then the string from the pool is returned. Otherwise, this String object is added to the pool, and a reference to it is returned.
Here is a java code demonstrating the above-mentioned concept.
For this example, 3 objects will be created, one in
heap
, other two in thestring pool
.
Here is the diagram showing the objects and it’s references.
Okay, that's all for this article. I am sure by now you understand the concept of string pool
in java and you know how objects are created in heap
and string pool
, using string literals
notation and new
keyword.
Let me know if you have any questions or suggestions in the comment section.