1. String Pool in Java
The String Pool is a special memory area in Java where String literals are stored and reused.
Java uses the String Pool to save memory and improve performance.
2. Why String Pool is Used
Without the String Pool, Java would create duplicate String objects repeatedly, which would waste memory.
- a. Reduces duplicate objects
- b. Saves memory
- c. Improves performance
- d. Reuses existing String literals
- e. Helps faster String comparison
3. Where String Pool is Used
The String Pool is used automatically whenever String literals are created.
- a. Login systems
- b. Web applications
- c. Configuration values
- d. Constant messages
- e. Database applications
- f. Enterprise Java systems
4. Understanding How String Pool Works
When Java creates a String literal, it first checks the String Pool.
- a. If the String already exists, Java reuses the existing object
- b. If the String does not exist, Java creates a new object inside the pool
4.1 Basic String Pool Example
public class Main {
public static void main(String[] args) {
String s1 = "Java";
String s2 = "Java";
System.out.println(s1 == s2);
}
}
The output is true because both variables point to the same object from the String Pool.
String literals automatically use the String Pool.
4.2 Memory Representation
Internally, only one "Java" object is created in the pool.
| Variable | Reference |
| s1 | Java Object |
| s2 | Java Object |
5. Strings Created Using new Keyword
Strings created using the new keyword do not directly reuse pool objects.
5.1 Example Using new
public class Main {
public static void main(String[] args) {
String s1 = new String("Java");
String s2 = new String("Java");
System.out.println(s1 == s2);
}
}
The output is false because two separate objects are created in heap memory.
Using new String() creates extra objects and increases memory usage.
5.2 Comparing Literal and new Object
public class Main {
public static void main(String[] args) {
String s1 = "Java";
String s2 = new String("Java");
System.out.println(s1 == s2);
}
}
The result is false because s1 points to the pool object while s2 points to a different heap object.
6. Internal Working of String Pool
The String Pool internally stores unique String literals only once.
6.1 Pool Lookup Process
When Java encounters a String literal:
- a. Java searches the String Pool
- b. If found, the existing reference is returned
- c. If not found, a new object is created
- d. The new object is added to the pool
6.2 Pool Reusability
Because Strings are immutable, Java can safely reuse the same object across multiple variables.
public class Main {
public static void main(String[] args) {
String a = "Code";
String b = "Code";
String c = "Code";
System.out.println(a == b);
System.out.println(b == c);
}
}
All variables point to the same pooled object.
7. Understanding intern() Method
Java provides the intern() method to manually place Strings into the String Pool.
7.1 Basic intern() Example
public class Main {
public static void main(String[] args) {
String s1 = new String("Java");
String s2 = s1.intern();
String s3 = "Java";
System.out.println(s2 == s3);
}
}
The output is true because intern() returns the pooled String reference.
intern() helps reuse existing pooled Strings instead of creating duplicate references.
7.2 Why intern() is Useful
- a. Reduces duplicate String objects
- b. Saves memory
- c. Improves String reuse
- d. Useful in large applications
8. Memory Optimization Using String Pool
The String Pool helps Java optimize memory usage automatically.
8.1 Without String Pool
If pooling did not exist, duplicate String literals would create multiple unnecessary objects.
8.2 With String Pool
Using pooled objects reduces heap memory consumption.
public class Main {
public static void main(String[] args) {
String s1 = "Apple";
String s2 = "Apple";
String s3 = "Apple";
System.out.println(s1 == s2);
System.out.println(s2 == s3);
}
}
Only one "Apple" object is stored in the pool and reused by all variables.
Applications with many repeated Strings benefit greatly from String Pool optimization.
9. String Comparison and String Pool
The behavior of == and equals() is strongly connected to the String Pool.
9.1 Using == Operator
The == operator compares memory references.
public class Main {
public static void main(String[] args) {
String s1 = "Java";
String s2 = "Java";
System.out.println(s1 == s2);
}
}
The output is true because both references point to the same pooled object.
9.2 Using equals() Method
The equals() method compares actual content.
public class Main {
public static void main(String[] args) {
String s1 = new String("Java");
String s2 = new String("Java");
System.out.println(s1.equals(s2));
}
}
The output is true because both Strings contain the same characters.
Use equals() when comparing String values instead of ==.
10. Common Beginner Mistakes
10.1 Assuming == Always Compares Content
Many beginners incorrectly use == for String comparison.
public class Main {
public static void main(String[] args) {
String a = new String("Code");
String b = new String("Code");
System.out.println(a == b);
}
}
The result is false because different objects are being compared.
10.2 Overusing new String()
Using new String() unnecessarily creates extra heap objects.
This reduces memory efficiency.
10.3 Confusing Heap Memory and Pool Memory
Pool objects and normal heap objects are different memory references even if they contain identical text.
11. Important Points to Remember
- a. String Pool stores unique String literals
- b. String literals reuse pooled objects
- c. new String() creates separate heap objects
- d. intern() returns pooled references
- e. == compares references
- f. equals() compares actual content
- g. String Pool improves memory optimization
The Java String Pool is an important memory optimization mechanism that helps reduce duplicate String objects and improve application performance. Understanding how pooled Strings behave makes it easier to write memory-efficient Java programs and correctly compare String objects.
