String Pool in Java: String is one of the most important discussed topics in Java. There are a lot of concepts related to the String and one of them is String pool in Java. In this section, we will discuss the two important concepts of String i.e. String pool or String Intern concept. The string pool in Java imitates the Flyweight design pattern.

What is a String Pool?

It is a well-known fact that a string is treated as an object in Java. In Java, a string object is always stored in the heap memory. When we create string, the JVM automatically creates a pool in the heap memory to store the string literals is called String pool. The Java String class takes care of the String pool, and by default, the String pool is empty. String Intern pool or String Constant pool are the other synonyms of the Java String pool.

The Need of String Pool

It is an obvious question that why the String pool is required when there is already a heap memory? The answer is, as the object allocation is always costly work in Java, as it enhances the time and space complexity. Therefore, the JVM (Java Virtual Machine) does the optimization from their end to reduce the time and space complexity. To achieve the same, an area inside the heap area is marked as the String pool.

Each and every time when a string literal is created in a Java program, the JVM checks in the String pool whether the string literal, which is going to be created, is present in the String pool or not. If the string literal is already present, the JVM returns the reference of that string literal; otherwise, it allocates the memory for the string literal inside the String pool. Thus, we can say that the String pool only contains the distinct string literals.

String Creation in Java

Following are the ways to create strings in Java.

By using String Literal

Let’s observe the Java program of the string literals.



Explanation: Before the start of the program, the String pool remains empty. So, for str1, the JVM allocates memory in the String pool. However, for st2, the JVM checks for the presence of the string literal “Tutorial & Example”. As the string literal is already present, the JVM does not allocate new memory for st2 and returns the reference of the already created string literal. Therefore, st1 and st2 have the same reference. Because of the case sensitivity feature of the Java language, the string literal of st3 is different from the st2 or st1. Hence, a new object is created for the reference variable st3. The following diagram depicts the same.

String Pool in Java

By using the new Keyword

Now, observe the program for the same.



Explanation: Creation of the string objects using the new keyword always allocated memory outside the string pool. The JVM never checks for the presence of the same strings. Therefore, objects are created for st1, as well as, st2 even though both contain the same string. The reference variable str3 is also treated in the same way. The following diagram represents the same.

String Pool in Java

The intern() method

The intern() method either refers to the already created object in the String pool, or it keeps the strings in the String pool. If the current string object is already present in the String pool, the intern() method returns the reference of it. It is determined by the equals() method. If the equals() method returns true, the reference is returned from the string pool; otherwise, an object is created in the string pool. Note that, even with the new keyword the intern() method puts the string literal in the String pool. Let’s confirm the same with the help of the following Java program.



Explanation: The first statement in the output is straightforward. Now, observe the second statement in the output. It says, “st2 and st3 do not have the same reference.” The reason behind it is that the calling of the intern() method for the st3. The intern() method puts the string in the String pool, whose reference is held by the st3. The following diagram depicts the same.

String Pool in Java

The third statement of the output shows the same reference because of the same reason. Note that the string st4 holds the reference of the string literal, which is always created in a string pool.

Note: The string literals in Java implicitly invokes the intern() method. Hence, they are always created in the String pool.

Pin It on Pinterest

Share This