For working professionals
For fresh graduates
More
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
46. Packages in Java
53. Java Collection
56. Generics In Java
57. Java Interfaces
60. Streams in Java
63. Thread in Java
67. Deadlock in Java
74. Applet in Java
75. Java Swing
76. Java Frameworks
78. JUnit Testing
81. Jar file in Java
82. Java Clean Code
86. Java 8 features
87. String in Java
93. HashMap in Java
98. Enum in Java
101. Hashcode in Java
105. Linked List in Java
109. Array Length in Java
111. Split in java
112. Map In Java
115. HashSet in Java
118. DateFormat in Java
121. Java List Size
122. Java APIs
128. Identifiers in Java
130. Set in Java
132. Try Catch in Java
133. Bubble Sort in Java
135. Queue in Java
142. Jagged Array in Java
144. Java String Format
145. Replace in Java
146. charAt() in Java
147. CompareTo in Java
151. parseInt in Java
153. Abstraction in Java
154. String Input in Java
156. instanceof in Java
157. Math Floor in Java
158. Selection Sort Java
159. int to char in Java
164. Deque in Java
172. Trim in Java
173. RxJava
174. Recursion in Java
175. HashSet Java
177. Square Root in Java
190. Javafx
In Java, strings are used everywhere — from form inputs to system messages. But storing so many strings can take up a lot of memory. That’s where the String Pool comes in. It helps Java save memory by reusing string literals instead of creating new ones every time.
Understanding how the String Pool works helps you write more efficient and optimized code, especially in large applications. This blog will walk you through the concept of the Java String Pool, how it works, its pros and cons, and where it’s most useful — all with simple examples and explanations. Also, you can explore upGrad’s Software Engineering Courses to understand the practical applications of such concepts.
The String Pool in Java programming is a special memory area inside the Java Method Area where string literals are stored. When you create a string using double quotes (" "), Java stores it in the pool and reuses it if the same string is created again.
Syntax:
String s1 = "hello";
String s2 = "hello"; // Reused from the pool
Both s1 and s2 point to the same object in memory.
Master in-demand skills with these popular training programs.
Java uses the String Pool mainly to save memory. Since strings are immutable, Java can safely store them in a shared pool and reuse them across your code. This improves performance and reduces memory usage in large-scale applications with repeated strings.
public class StringPoolDemo {
public static void main(String[] args) {
String s1 = "Java";
String s2 = "Java";
System.out.println(s1 == s2);
}
}
Output:
True
Explanation: Both s1 and s2 refer to the same object in the String Pool, so == returns true.
Checkout: What are String Functions in Java
When you create a string like "Java", the JVM checks the pool. If the string exists, it returns the reference. If not, it adds the string to the pool. This is only true for strings created using literals, not the new keyword.
Let’s compare how == behaves with and without the String Pool.
public class StringCompare {
public static void main(String[] args) {
String s1 = "Java";
String s2 = new String("Java");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
}
Output:
false
true
Explanation: == checks reference (different), while .equals() checks value (same). Only literals go to the pool by default.
Also explore: Strings in Java Vs Strings in Cpp
Java provides the intern() method to manually add a string to the pool. If the string is already present, it returns the existing reference; otherwise, it adds it.
Syntax:
String original = new String("Hello");
String interned = original.intern();
Example:
public class InternDemo {
public static void main(String[] args) {
String s1 = new String("Hello");
String s2 = s1.intern();
String s3 = "Hello";
System.out.println(s2 == s3);
}
}
Output:
true
Explanation: intern() adds the string to the pool, so s2 and s3 refer to the same object.
Strings created with the new keyword are stored in the heap, while string literals are stored in the String Pool. The pool is a subset of the method area memory and uses less space than the heap.
Example:
String a = "Java"; // In string pool
String b = new String("Java"); // In heap memory
Explanation: Although a and b have the same content, they live in different parts of memory.
Must read: Difference Between Stack and Heap Memory in Java
Must read: String length in Java
The String Pool improves memory efficiency across various real-world Java applications:
The String Pool in Java is a smart memory-saving feature. By reusing string literals, it reduces memory consumption and speeds up execution. Understanding how it works can help you write cleaner, more efficient code. Use it wisely and consider intern() when needed.
Java stores strings in a pool to save memory and improve performance. When multiple parts of a program use the same string literal, Java reuses one object from the pool instead of creating a new one every time.
The intern() method forces a string to be added to the pool. If the string already exists in the pool, it returns the reference to that object. This helps reduce memory use when many identical strings are created at runtime.
No, using new String("abc") creates a new object in the heap, even if "abc" exists in the pool. You need to call .intern() explicitly on that string to move it into the pool or reuse the pooled version.
Yes, but only if you explicitly call the .intern() method. Runtime-created strings like through StringBuilder or user input are stored in the heap unless interned manually.
Before Java 7, the String Pool was part of the PermGen memory (method area). Since Java 7, it resides in the main heap memory, allowing better flexibility and fewer memory overflow issues.
No, the String Pool is a unique feature for the String class because strings are immutable. Other types like Integer or Float may benefit from autoboxing and caching but don’t have a dedicated pool like Strings.
Only compile-time constant concatenations (e.g., "a" + "b") are stored in the pool. If strings are joined at runtime (e.g., using variables or + with input), they’re not pooled unless you use the .intern() method.
Not automatically. When you use .toString() on a StringBuilder, it creates a new heap object. To store it in the String Pool, you must call .intern() on the resulting string.
String literals usually stay in memory for the entire life of the application. However, dynamically interned strings without references can be garbage collected if the JVM determines they’re no longer needed.
In large apps with repeated strings (e.g., log messages, UI text), the pool avoids creating multiple copies of the same string. This reduces memory use and speeds up comparisons since string references can be compared directly.
It depends. String Pooling helps when the same string values repeat often. But unnecessary interning of unique strings can increase memory usage and slow performance. Use it wisely in high-volume, repeat-pattern scenarios like caching or file parsing.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author|900 articles published
Previous
Next
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.