Course contentsShow
← Java
Lesson 2176 of 2,87043. String Handling in DepthFree lesson

What is the String Intern Pool?

Understanding the JVM's special memory area for storing unique string literals to save memory and enable fast equality checks.

What is the String Intern Pool?

You'll learn what the String Intern Pool is and how the JVM uses it to optimize memory when working with string literals.

The Concept

The String Intern Pool (also called the "string pool" or "string constant pool") is a special memory area maintained by the JVM that stores a single copy of each unique string literal. When you create string literals in your code, the JVM checks this pool first. If the exact string already exists, it reuses that reference instead of creating a duplicate object.

Think of it like a library's catalog system: instead of buying multiple copies of the same book for different readers, the library keeps one copy and multiple people can reference it. This saves physical space (memory) and makes it easy to check if two people want the same book (identity comparison).

How It Works

When you write string literals in your source code:

String s1 = "hello";
String s2 = "hello";

Both s1 and s2 point to the same object in the intern pool, not two separate objects. This is why identity comparison works:

String s1 = "hello";
String s2 = "hello";
System.out.println(s1 == s2); // true — same reference!

The intern pool enables fast equality checks using == for string literals and significantly reduces memory usage when the same strings appear repeatedly in your application.

Manual Interning

You can explicitly add strings to the pool using the intern() method, but that's a future topic.

Key Takeaway: The String Intern Pool is the JVM's memory-efficient storage for unique string literals, allowing multiple variables to share the same string object and enabling fast identity-based comparisons.