String Pool in Java is a special memory region in the heap that stores string literals. When a string literal is created, Java checks whether an identical string already exists in the pool. If it does, a reference to the existing string is returned instead of creating a new object, thereby optimizing memory usage.
- When a string literal is created, Java checks the String Pool for an existing instance.
- If the string is found, the existing reference is returned.
- If not, a new string object is created and added to the pool.
- Strings created using the
newkeyword do not use the pool by default, but they can be added manually using theintern()method.
public class StringPoolExample {
public static void main(String[] args) {
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
String s4 = s3.intern();
System.out.println(s1 == s2); // true (same reference from pool)
System.out.println(s1 == s3); // false (different objects)
System.out.println(s1 == s4); // true (s4 is interned, points to pool)
}
}- Memory Efficiency: Reduces memory consumption by reusing immutable string objects.
- Performance Optimization: Reduces the number of objects created, improving efficiency.
If a string is created using new String(), it resides in the heap and not in the pool. To store it in the pool and get its reference, use intern():
String s = new String("Java").intern();The String Pool mechanism in Java helps optimize memory usage by reusing immutable string literals. Understanding how it works is essential for efficient Java programming.