What is String Pool in Java?
Learn Java String Pool: memory optimization technique where JVM stores string literals in heap memory for efficient reuse. Discover how string pool works, literal vs new keyword differences, performance benefits & memory management from Java 7 onward.
The Java String Pool (also known as the String Intern Pool) is a special memory area in the Java heap where string literals are stored to optimize memory usage.
Note: Only string literals are stored in String pool. 😉
When a string is created in Java, especially through literals, the Java Virtual Machine (JVM) uses the String Pool to reduce memory overhead by reusing existing strings instead of creating new ones.
String pool before and after Java 7
Before Java 7 (in JDK 6 and earlier), The String Pool was part of the PermGen (Permanent Generation) space, a fixed-size memory area. This limited the size of the pool, and large numbers of strings could result in an OutOfMemoryError
.
From Java 7 onward: The String Pool was moved to the heap memory, making it more flexible in size and less prone to memory errors.
How does String pool work?
Two ways are discussed below that explain how Java uses String pool.
- Using String literal
- Using String constructor.
String literal creation
When a string literal is created in Java, the JVM first checks the String Pool to see if an identical string already exists.
- If the string exists in the pool, the reference to the existing string is returned.
- If the string does not exist, the JVM creates a new string in the pool and returns a reference to it.
This means that string literals are interned (shared) automatically.
==
operator here compares the references of two objects, checking if they point to the exact memory location.
Code
public class StringPool {
public static void main(String[] args) {
String first = "Hello world";
String second = "Hello world"; // `second` points to the same string object as `first`
System.out.println(first == second); // true, both refer to the same object
}
}
Memory sketch (stack, heap areas)

String constructor using new keyword
Creating a string using the new
keyword creates a new string object on the heap, even if an identical string already exists in the String Pool.
Code
public class StringPool {
public static void main(String[] args) {
String third = "Hello world"; // created inside string pool area in heap memory
String fourth = new String("Hello world"); // fourth creates a new object in heap
System.out.println(third == fourth); // false, different references
System.out.println(third.equals(fourth)); // true, same objects
}
}
Memory sketch (stack, heap areas)

Key benefits
- Memory Efficiency: Java conserves memory by reusing strings, especially when dealing with large numbers of string literals. For example, if you use
"Hello"
in multiple places in your code, instead of creating multiple"Hello"
objects in memory, the JVM will only store one instance in the String Pool and refer to it multiple times. - Performance Optimization: Since strings are immutable, sharing references to the same string object does not pose a risk of accidental modification. Therefore, the String Pool helps improve memory usage and performance by avoiding duplicate string objects.
Important Points to Remember:
- Strings created with string literals are automatically added to the String Pool.
- Strings created with the
new
keyword are stored in the heap, but they can be manually added to the pool usingintern()
. - The String Pool is designed for optimization and memory efficiency in Java, particularly when working with a large number of string literals.
The Java String Pool is a crucial memory optimization technique that reduces redundancy and improves performance by ensuring that identical string literals share the same memory space. Understanding how it works can help you write more efficient Java code.