Java String Constant Pool

In this blog post, we will learn everything about String Constant Pool in Java with examples.

What is String Constant Pool?

The Java String Constant Pool is a special memory region where the Java Virtual Machine (JVM) stores string constants. This area of memory is part of the Java heap space, and its main advantage is that it allows the JVM to optimize the amount of space needed for storing strings by reusing common ones.

How String Constant Pool Works 

When a string is created in Java, the JVM first checks the string constant pool. If a string with the same value already exists in the pool, a reference to that string is returned. Otherwise, a new string object is created in the pool, and a reference to this new object is returned. This process is called "interning."

For example:
String str1 = "JavaGuides";  // These two strings are both references
String str2 = "JavaGuides";  // to the same object in the constant pool
In the above example, the JVM will only create one object in the string constant pool. Both str1 and str2 will point to the same object in the pool.

String Constant Pool with new Keyword 

If we create a string using the new keyword, it will always create a new object in the heap space, but it also checks the string constant pool. If a string with the same value is found in the pool, the newly created string object in the heap will have the same value. However, if a string with the same value is not found in the pool, a new string will also be created in the pool.
String str1 = new String("JavaGuides"); // creates one string object in the heap,
                                        // and one string in the string constant pool
String str2 = new String("JavaGuides"); // creates a second string object in the heap,
                                        // but reuses the string in the constant pool
In this case, str1 and str2 refer to two different objects in the heap, but the strings in the constant pool are still the same object.

Why use the String Constant Pool 

The string constant pool helps save memory when your program creates many strings. Because strings are immutable in Java, the JVM can safely reuse them without worrying about the value being changed. 

It's also important to note that the == operator checks for reference equality. Two string references are equal if and only if they point to the exact same object, which can only happen through string interning.

The intern() Method 

In Java, the intern() method of the String class can be used to find or create an equivalent String in the String Constant Pool. When the intern() method is called on a String object, it checks if a String with the same value is already present in the pool. If there is, it returns the reference to the pooled instance. If not, it adds the String to the pool and returns its reference. 

Let's illustrate this with an example:
public class Main {
    public static void main(String[] args) {
        String str1 = new String("JavaGuides"); // creates a new string object in heap and 
                                                // the literal "JavaGuides" in the string constant pool
        String str2 = "JavaGuides"; // refers to the instance in the string constant pool
        String str3 = str1.intern(); // str1 content is added to the string constant pool 
                                     // and its reference is returned

        System.out.println(str1 == str2); // Outputs "false"
        System.out.println(str2 == str3); // Outputs "true"
    }
}
In the above example, str1 == str2 is false because str1 refers to the instance in the heap and str2 refers to the instance in the string constant pool. They are not the same instance. 

On the other hand, str2 == str3 is true because the intern() method makes str3 refer to the instance in the string constant pool (which is the same as str2), hence they are the same instance. 

So, the intern() method allows you to optimize your memory usage by having equivalent strings refer to the same instance in the string constant pool instead of each having its separate instances.

Summary 

The Java String Constant Pool is a powerful feature of the JVM that optimizes the use of memory by reusing common string objects. It plays a crucial role because of the immutability of string objects in Java. The JVM ensures that a string is only stored once in the pool, saving memory and increasing efficiency. This is particularly useful in programs that create many strings, where the savings in memory can be quite significant. 

Remember, a string created without using the new keyword goes directly to the string constant pool, but if it’s created with the new keyword, it's placed in the heap memory. If you want to ensure a string object created using a new keyword is also placed in the string pool, you can use the intern() method.

Java String Related Posts

Comments