Backend Development 12 min read

Understanding How new String Creates Objects and the Role of the String Constant Pool in Java

This article explains why the new String("xxx") expression may create one or two objects depending on the presence of the literal in the JVM's string constant pool, demonstrates the behavior with compiled bytecode and runtime examples, and clarifies common interview misconceptions about Java string object creation.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Understanding How new String Creates Objects and the Role of the String Constant Pool in Java

All Java developers have encountered the interview question about how many objects are created by new String("xxx") ; opinions vary between one object, two objects, or sometimes one and sometimes two, leading to confusion.

The differing answers stem from the behavior of the String Constant Pool . Some claim that the new operation creates an object in the pool, while others say it only creates a heap object and interacts with the pool only when intern() is called.

String Constant Pool is a special area in the JVM that stores unique string literals to reduce memory usage and improve performance. When a literal is used, the JVM first checks the pool; if the string exists, the reference is reused, otherwise a new entry is added.

Example demonstrating literal sharing:

public class StringExample {
    public static void main(String[] args) {
        String s1 = "Java";
        String s2 = "Java";
        System.out.println(s1 == s2);
    }
}

The program prints true , indicating both variables point to the same object in the constant pool.

Memory layout changes from JDK 1.7 (metaspace replaces perm‑gen and the pool moves to the heap) to JDK 1.8 (perm‑gen removed entirely) are illustrated with diagrams.

Answer analysis : If the literal already exists in the pool, new String("xxx") creates only the heap object (1 object). If the literal is absent, the JVM creates the pool entry first and then the heap object (2 objects). Therefore the correct answer is "one or two objects" depending on the pool state.

Technical proof using bytecode:

public class StringExample {
    public static void main(String[] args) {
        String s1 = new String("javaer-wang");
        String s2 = "wang-javaer";
        String s3 = "wang-javaer";
    }
}

Compiling with javac and inspecting with javap -v shows the constant pool entries for the literals, confirming that the literal is placed in the pool at compile time.

Running the following code:

String s1 = new String("javaer-wang");
String s2 = new String("javaer-wang");
System.out.println(s1 == s2);

outputs false because each new creates a distinct heap object, even though the underlying literal exists in the pool.

Further examples illustrate compiler optimizations where concatenated compile‑time constants are folded into a single pool entry, resulting in true for comparisons that would otherwise be false .

In summary, using javap -v reveals that new String may create one or two objects: one when the literal is already in the constant pool, and two when it is not. The article also covers the evolution of the pool in JDK 1.7/1.8 and how the compiler optimizes constant strings.

Finally, the author encourages readers to like and share the article if they found it helpful.

JavaJVMperformanceObject CreationString()Constant Pool
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

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.