How Many String Objects Does new String("abc") Actually Create? A Deep Dive into Java Memory
This article dissects the Java String creation process, explaining how the constant pool and heap interact, why new String("abc") can generate one or two objects, and how string concatenation and StringBuilder affect the total number of objects created during runtime.
Java interview series – this second article examines a classic question: how many objects are created by the statement new String("abc"). Opinions vary between one and two objects, but the true answer depends on the underlying JVM mechanics.
Underlying Mechanism
When a string literal like "abc" is assigned directly, the value is stored in the string constant pool. If the literal already exists, no new object is created; otherwise, one object is added to the pool and the reference is assigned.
Using new String("abc") first checks the constant pool. If the literal is absent, the JVM creates a pool entry for "abc" and then creates a separate String object on the heap that references the same character array, resulting in two objects. If the literal already exists, only the heap object is created, totaling one object.
The pool lookup uses String.intern(): public native String intern(); Illustrative diagrams (shown below) depict the two storage modes, with the heap object's char[] value pointing to the same array in the constant pool.
Deeper Interview Questions
If the interview question presents only new String("abc"), the answer can be two objects, but it also depends on whether the constant pool already contains "abc" from other classes loaded at startup.
String Concatenation and Object Count
When concatenating literals, e.g., "abc" + "def", the compiler folds them into a single constant "abcdef", creating just one object and avoiding temporary strings.
For mixed concatenation like "abc" + new String("def"), the JVM generates a StringBuilder at runtime. The process creates:
Two literals in the constant pool ( "abc" and "def").
A heap String object for new String("def").
A StringBuilder instance.
The final concatenated String produced by StringBuilder.toString(), which internally creates another String via new String(value, 0, count).
Thus, the total is four String objects plus one StringBuilder (five objects). The final string resides on the heap, not in the constant pool.
@Test
public void testString3() {
String s1 = "abc";
String s2 = new String("def");
String s3 = s1 + s2;
String s4 = "abcdef";
System.out.println(s3 == s4); // false
}Debugging shows that s3 and s4 have identical character content but different value array references, confirming that s3 lives on the heap.
@Override
public String toString() {
// Create a copy, don't share the array
return new String(value, 0, count);
}Conclusion
By analyzing a single line of string creation and various concatenation scenarios, we uncover how Java manages string objects in the constant pool, heap, and through StringBuilder. Understanding these details equips you to answer interview questions accurately and recognize underlying JVM behavior.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
