How Many Objects Does new String("abc") Actually Create?
This article explains why the interview question "String str = new String(\"abc\")" can create either one or two objects depending on JVM string pool state, detailing the JVM memory model, string pool mechanics, code examples, best practices, and common misconceptions.
Interview Focus Points
JVM memory model : understanding method area/metaspace and heap, especially the string constant pool.
String creation mechanisms : the difference between new String(...) and literal assignment.
String pool (String Pool) : how it works and the role of intern().
Logical reasoning : analyzing when and where objects are created during code execution.
Core Answer
The answer to the question is: 1 or 2 objects .
Specifically:
In the usual case, two objects are created:
Object 1 (heap) : the new String(...) call creates a new String instance on the heap.
Object 2 (String pool) : the literal "abc" causes the JVM to look for or create a corresponding entry in the string constant pool.
In a special case, only one object is created when the literal "abc" already exists in the pool before the code runs; the literal then references the existing pool object, and only the heap object from new String(...) is created.
Deep Analysis
Principle/Mechanism
String constant pool : a special memory area designed by the JVM to improve performance and reduce memory usage. Before JDK 7 it resided in the method area; from JDK 7 onward it lives in the heap. When a string literal like "abc" is encountered, the JVM first checks the pool:
If an equal string exists, the existing reference is returned.
If not, a new pool object is created and its reference is returned.
new String(String original) constructor : creates a brand‑new String object on the heap that copies (or references, depending on implementation) the character array of the argument. The new operation always results in a new heap object.
Code Example and Memory Analysis
// Scenario 1: creates 2 objects
public class StringCreationDemo {
public static void main(String[] args) {
// First appearance of "abc"
String str = new String("abc");
// 1. Literal "abc" not in pool → pool creates O1
// 2. new String allocates O2 on heap
// 3. Constructor copies characters from O1 to O2
// 4. str references O2
}
}
// Scenario 2: creates 1 object (pool already contains "abc")
public class StringCreationDemo2 {
public static void main(String[] args) {
String constant = "abc"; // pool now has O_pool
// ... other code
String str = new String("abc"); // literal reuses O_pool, only heap object O_heap is created
}
}Comparison and Best Practices
String str = "abc"vs String str = new String("abc") Literal approach : may create 0 or 1 object (only when the literal is absent from the pool). All identical literals share the same pool object, saving memory and offering better performance.
new approach : always creates a new heap object, which is distinct from the pool object (though equals() returns true, == is false).
Best practice : In most business code, prefer the literal form String s = "abc". Use new String(...) only when a truly distinct object is required, which is a rare scenario.
Common Pitfalls
Confusing reference and object : the variable str is a reference, not an object itself. The interview asks about object creation count.
Ignoring existing pool entries : answering "always 2" overlooks the JVM's caching mechanism.
Misunderstanding intern() : when the pool already contains the string, new String("abc").intern() returns the existing pool reference and does not create a new object; the temporary heap object may be garbage‑collected if unreferenced.
Conclusion
In most first‑time literal scenarios, String str = new String("abc") creates two objects—one in the string pool and one on the heap. If the literal already exists in the pool, only the heap object is created.
Java Architect Handbook
Focused on Java interview questions and practical article sharing, covering algorithms, databases, Spring Boot, microservices, high concurrency, JVM, Docker containers, and ELK-related knowledge. Looking forward to progressing together with you.
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.
