Java.Core.What is a “string pool”?

What is a String Pool in Java?

The String Pool (also known as the String Intern Pool) is a special memory area in the heap where Java stores string literals to optimize memory usage and improve performance.

  • When you create a string literal, Java automatically adds it to the String Pool.
  • If the same string already exists in the pool, Java reuses the existing object instead of creating a new one.
  • The String Pool saves memory and makes string operations more efficient.

How Does the String Pool Work?

1. String Literals Are Stored in the Pool

public class StringPoolExample {
    public static void main(String[] args) {
        String s1 = "Java"; // Stored in the String Pool
        String s2 = "Java"; // Reuses the same object from the pool
        
        System.out.println(s1 == s2); // true (same reference)
    }
}

👉 Explanation:

  • "Java" is stored in the String Pool.
  • s1 and s2 both point to the same object, so s1 == s2 is true.

2. Strings Created Using new Are NOT in the Pool

public class StringHeapExample {
    public static void main(String[] args) {
        String s1 = new String("Java"); // Creates a new object in Heap
        String s2 = new String("Java"); // Creates another new object in Heap
        
        System.out.println(s1 == s2); // false (different objects)
        System.out.println(s1.equals(s2)); // true (same content)
    }
}

👉 Explanation:

  • new String("Java") creates a new object in heap memory, not in the pool.
  • Even though both objects contain the same text ("Java"), they are different objects (s1 != s2).

3. Using intern() to Store in the Pool

The intern() method forces a string to be stored in the String Pool, ensuring that all identical strings share the same memory.

public class StringInternExample {
    public static void main(String[] args) {
        String s1 = new String("Java");
        String s2 = s1.intern(); // Stores "Java" in the pool (if not already present)

        String s3 = "Java"; // Reuses the same pooled string
        
        System.out.println(s2 == s3); // true (same reference)
    }
}

👉 Explanation:

  • s1.intern() adds "Java" to the String Pool and returns the pooled reference.
  • s3 is a literal, so it also uses the pooled reference.
  • s2 == s3 is true because both point to the same object in the pool.

Advantages of the String Pool

Memory Efficiency – Instead of creating multiple string objects with the same value, Java reuses existing strings, saving heap memory.
Performance Boost – Since comparing string references (==) is faster than comparing content (.equals()), the pool helps improve performance.
Automatic Optimization – Java automatically manages the pool, ensuring efficient memory usage.


When to Use the String Pool

🔹 Use string literals ("Hello") whenever possible to take advantage of the pool.
🔹 Use intern() if you need to manually ensure that a string is in the pool.
🔹 Avoid excessive string concatenation, as it creates new objects (use StringBuilder instead).


Key Takeaways

FeatureString PoolHeap (new String())
Memory EfficiencyHigh (Reuses existing objects)Low (Creates new objects)
Reference Comparison (==)Works (true for same strings)Fails (false for same content)
Stored in Pool?Yes (if created as a literal)No (unless intern() is used)
ExampleString s = "Java";String s = new String("Java");

Conclusion

  • The String Pool is an optimization feature that stores and reuses string literals.
  • String literals are automatically added to the pool, but new String("text") creates a new heap object.
  • Use intern() to manually store a string in the pool.
  • The String Pool helps save memory and improves performance.
This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.