What is the String Pool in Java?
The String Pool (String Intern Pool) is a special memory area in the Java Heap where String literals are stored to save memory and improve performance.
How String Pool Works
public class StringPoolExample {
public static void main(String[] args) {
// String literals - stored in String Pool
String s1 = "Hello";
String s2 = "Hello";
// s1 and s2 refer to the SAME object in pool
System.out.println("s1 == s2: " + (s1 == s2)); // true (same reference)
System.out.println("s1.equals(s2): " + s1.equals(s2)); // true
// String with new keyword - stored in Heap (outside pool)
String s3 = new String("Hello");
System.out.println("s1 == s3: " + (s1 == s3)); // false (different references)
System.out.println("s1.equals(s3): " + s1.equals(s3)); // true
// Force string to pool using intern()
String s4 = s3.intern();
System.out.println("s1 == s4: " + (s1 == s4)); // true (now in pool)
// Check memory addresses (conceptual)
System.out.println("s1 address: " + System.identityHashCode(s1));
System.out.println("s2 address: " + System.identityHashCode(s2));
System.out.println("s3 address: " + System.identityHashCode(s3));
System.out.println("s4 address: " + System.identityHashCode(s4));
}
}
String Pool Memory Demonstration
public class MemoryDemo {
public static void main(String[] args) {
// All three reference the SAME object in String Pool
String literal1 = "Java";
String literal2 = "Java";
String literal3 = "Java";
System.out.println("Literals point to same object:");
System.out.println("literal1 == literal2: " + (literal1 == literal2)); // true
System.out.println("literal2 == literal3: " + (literal2 == literal3)); // true
// Heap objects - different objects each time
String heap1 = new String("Java");
String heap2 = new String("Java");
String heap3 = new String("Java");
System.out.println("
Heap objects are different:");
System.out.println("heap1 == heap2: " + (heap1 == heap2)); // false
System.out.println("heap2 == heap3: " + (heap2 == heap3)); // false
// After interning - they refer to pool object
String interned1 = heap1.intern();
String interned2 = heap2.intern();
String interned3 = heap3.intern();
System.out.println("
After interning - all refer to same object:");
System.out.println("interned1 == literal1: " + (interned1 == literal1)); // true
System.out.println("interned2 == literal1: " + (interned2 == literal1)); // true
System.out.println("interned3 == literal1: " + (interned3 == literal1)); // true
}
}
String Concatenation and Pool
public class ConcatenationDemo {
public static void main(String[] args) {
// Compile-time constant - goes to pool
String s1 = "Hello" + "World";
String s2 = "HelloWorld";
System.out.println("Compile-time constant:");
System.out.println("s1 == s2: " + (s1 == s2)); // true (both in pool)
// Runtime concatenation - new object in heap
String hello = "Hello";
String world = "World";
String s3 = hello + world;
String s4 = "HelloWorld";
System.out.println("
Runtime concatenation:");
System.out.println("s3 == s4: " + (s3 == s4)); // false (different objects)
System.out.println("s3.equals(s4): " + s3.equals(s4)); // true
// Using final variables - compile-time constant
final String f1 = "Hello";
final String f2 = "World";
String s5 = f1 + f2;
String s6 = "HelloWorld";
System.out.println("
With final variables (compile-time):");
System.out.println("s5 == s6: " + (s5 == s6)); // true
// Using StringBuilder - always creates new object
StringBuilder sb = new StringBuilder();
sb.append("Hello");
sb.append("World");
String s7 = sb.toString();
System.out.println("StringBuilder result:");
System.out.println("s7 == s4: " + (s7 == s4)); // false
}
}
String Pool Size and Performance
public class PoolPerformance {
public static void main(String[] args) {
int iterations = 100000;
// Without pool (heap only)
long start = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
String s = new String("Hello");
}
long heapTime = System.currentTimeMillis() - start;
// With pool (reusing literals)
start = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
String s = "Hello";
}
long poolTime = System.currentTimeMillis() - start;
System.out.println("Performance Test (" + iterations + " iterations):");
System.out.println("Heap (new String) time: " + heapTime + "ms");
System.out.println("Pool (literal) time: " + poolTime + "ms");
System.out.println("Pool is " + (heapTime - poolTime) + "ms faster");
// Memory usage demonstration
System.out.println("
Memory Usage:");
Runtime runtime = Runtime.getRuntime();
runtime.gc();
long before = runtime.totalMemory() - runtime.freeMemory();
// Creating many heap strings
for (int i = 0; i < 10000; i++) {
new String("Test");
}
long after = runtime.totalMemory() - runtime.freeMemory();
System.out.println("Heap strings memory: " + (after - before) / 1024 + " KB");
runtime.gc();
before = runtime.totalMemory() - runtime.freeMemory();
// Using literals (reused)
for (int i = 0; i < 10000; i++) {
String s = "Test";
}
after = runtime.totalMemory() - runtime.freeMemory();
System.out.println("Pool literals memory: " + (after - before) / 1024 + " KB");
}
}
Best Practices
public class BestPractices {
public static void main(String[] args) {
// DO: Use string literals when possible
String good1 = "Hello";
String good2 = "Hello";
// DON'T: Use new String() unnecessarily
String bad = new String("Hello"); // Creates extra object in heap
// DO: Use intern() for duplicate strings from external sources
String external = new String("DataFromAPI");
String pooled = external.intern(); // Adds to pool if not already there
// Example: Reading from external source
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
String input = reader.readLine();
// Intern input if you expect many duplicates
String internedInput = input.intern();
System.out.println("Input interned: " + internedInput);
} catch (IOException e) {
e.printStackTrace();
}
// Example: Status codes
String statusSuccess = "SUCCESS".intern();
String statusError = "ERROR".intern();
// String pool configuration
// Can be configured with JVM argument: -XX:StringTableSize=N
// Default size: 60013 (Java 8+)
// Increase size for applications with many unique strings
System.out.println("
String Pool Best Practices:");
System.out.println("1. Use string literals for constant strings");
System.out.println("2. Avoid new String() for simple strings");
System.out.println("3. Use intern() for repeated strings from external sources");
System.out.println("4. Configure StringTableSize for large applications");
System.out.println("5. Be careful with intern() in memory-constrained environments");
}
}
Optimize Java memory with String Pool knowledge from Online Learner!
0
likes
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
