Understanding the Java String Pool
Why "apple" == "apple" is true in Java?

1. What Makes String Special in Java
In Java, String behaves differently from most other objects. One of the main reasons is the String Pool, a special memory area used to optimize memory usage.
Since strings are used very frequently in applications, Java stores string literals in a shared pool so that identical strings can be reused instead of creating new objects every time.
This optimization helps reduce memory usage and improve performance.
2. The Java String Pool
2.1. What is the String Pool
The String Pool is a special area inside the JVM where string literals are stored.
When the JVM encounters a string literal, it checks whether the same string already exists in the pool.
If it exists, the JVM reuses the existing object
If it does not exist, the JVM creates a new string object in the pool
Example
String a = "apple";
String b = "apple";
System.out.println(a == b); // Output: true
System.out.println(a.equals(b)); // Output: true
Explanation
Both a and b reference the same object in the String Pool, so == returns true.
Conceptually
String Pool
|
|--- "apple"
↑ ↑
a b
3. String Created with new
When a string is created using the new keyword, Java always creates a new object in the heap, even if the same value already exists in the String Pool.
Example
String a = "apple";
String b = new String("apple");
System.out.println(a == b); // Output: false
System.out.println(a.equals(b)); // Output: true
Explanation
areferences the string in the String Poolbreferences a new object in the heap
Conceptually
Heap
|
|--- new String("apple") ← b
String Pool
|
|--- "apple" ← a
This is why == returns false, but equals() returns true.
4. The intern() Method
Java provides the intern() method to explicitly place a string into the String Pool.
If the string already exists in the pool, the existing reference is returned.
Example
String a = new String("apple");
String b = a.intern();
String c = "apple";
System.out.println(b == c); // Output: true
Explanation
ais created in the heapintern()returns the reference from the String Poolcalso references the pooled string
Conceptually
Heap
|
|--- "apple" (a)
String Pool
|
|--- "apple"
↑ ↑
b c
5. Why the String Pool Exists
The String Pool exists to reduce memory usage and improve efficiency. Imagine a large application using the same strings repeatedly.
Without pooling, each occurrence would create a new object. With the String Pool, identical literals reuse the same object, saving memory and reducing object creation overhead.
6. Best Practices
6.1. Use string literals when possible
Using string literals allows the JVM to reuse objects from the String Pool.
String name = "apple"; // uses String Pool
6.2. Avoid unnecessary new String()
String name = new String("apple"); // usually unnecessary
This creates a new object even when the same string already exists.
6.3. Use equals() for string comparison
Even though == may sometimes return true due to pooling, string comparisons should use equals().
String a = "apple";
String b = new String("apple");
System.out.println(a.equals(b)); // Output: true
✨ Conclusion
The Java String Pool is a special memory optimization that stores string literals in a shared pool. When identical literals appear, the JVM reuses the existing object instead of creating a new one.
This is why "apple" == "apple" can return true, while strings created with new String() are separate objects in the heap.
Understanding how the String Pool works helps developers avoid confusion when comparing strings and write more efficient Java code.





