☕ Java
Beginner
What is the String pool in Java?
Answer
The String pool (or String constant pool) is a special memory area in the Java heap where String literals are stored and reused. When you create a String literal like String s = "hello";, the JVM first checks if "hello" already exists in the pool. If it does, it returns a reference to that existing object instead of creating a new one — saving memory. Two literals with the same value will point to the same object in the pool: String a = "hello"; String b = "hello"; a == b; // true. Strings created with new String("hello") always create a new object outside the pool. Use intern() to manually add a String to the pool.
Previous
What is abstraction in Java?
Next
What is the difference between final, finally, and finalize in Java?