Backend Development 7 min read

Understanding Object Creation in Java: How Many Objects Are Created by `String s = new String("xyz")`?

This article explains the evolution of the Java method area, analyzes bytecode generated by `String s = new String("xyz")` and `String s = "xyz"`, and clarifies why the expression creates one or two objects depending on whether the literal already exists in the constant pool.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Understanding Object Creation in Java: How Many Objects Are Created by `String s = new String("xyz")`?

Every Java learner has encountered the classic interview question about how many objects are created by the statement String s = new String("xyz") . This article reviews the evolution of the method area and constant pool across JDK versions to provide a consistent basis for analysis, using JDK 1.8 as the reference.

Method Area Overview

Before JDK 1.7, the constant pool resided in the method area, which was a logical part of the heap often referred to as the "non‑heap". In JDK 1.7 the string constant pool was moved to the heap, and from JDK 1.8 onward the permanent generation (PermGen) was removed; the method area concept remained but its implementation changed to Metaspace, while the constant pool stayed in the heap.

Example 1: String s = "xyz";

public class Test {
    public static void main(String[] args) {
        String s = "xyz";
    }
}

Compiling with javac and disassembling with javap -c Test shows the ldc instruction loading the literal "xyz" from the constant pool onto the operand stack, followed by astore storing the reference in a local variable.

Example 2: String s = new String("xyz");

public class Test {
    public static void main(String[] args) {
        String s = new String("xyz");
    }
}

The bytecode reveals that ldc still loads the literal "xyz" from the constant pool, then new creates a new String object, invokespecial calls the constructor, and astore_1 stores the new object reference.

Thus, the statement creates two objects: the literal "xyz" (if it does not already exist in the pool) and the new String instance. Some answers count the variable reference as a third object, but a reference is not an object.

Combined Example

public class Test {
    public static void main(String[] args) {
        String s = "xyz";
        String s2 = new String("xyz");
    }
}

Bytecode shows that the second ldc does not create a new literal; it reuses the existing constant‑pool entry, confirming that only one literal object is created regardless of how many new String calls are made.

Common Bytecode Mnemonics

nop : does nothing.

aconst_null : pushes null onto the stack.

iconst_i : pushes integer i onto the stack (similarly lconst_0 , fconst_0 for long and float).

ldc : pushes a constant (int, float, or String) from the constant pool.

iload : pushes a local int variable onto the stack.

istore / astore_i : stores the top stack value into a local variable.

dup : duplicates the top stack value.

invokevirtual : calls an instance method.

invokespecial : calls a constructor, private method, or super method.

invokestatic : calls a static method.

invokeinterface : calls an interface method.

invokedynamic : invokes a dynamically linked method.

new : creates a new object and pushes its reference.

Conclusion

The number of objects created by String s = new String("xyz") depends on whether the literal "xyz" is already present in the constant pool and whether the reference variable is counted as an object:

If the literal does not exist and the reference is counted, three objects are created.

If the literal does not exist and the reference is not counted, two objects are created.

If the literal already exists and the reference is counted, two objects are created.

If the literal already exists and the reference is not counted, only one object (the new String ) is created.

In practice, references are not considered objects, so the answer is typically one or two objects, making this question unsuitable for junior‑level interviews.

backendJavabytecodeObject CreationString()Constant Pool
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.