Fundamentals 4 min read

Why Does new String("abc") Create One or Two Objects in Java?

Creating a String with new String("abc") can generate either one or two objects depending on whether the literal "abc" already exists in the JVM’s string constant pool, affecting object placement in the pool versus the heap and influencing reference equality comparisons.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Why Does new String("abc") Create One or Two Objects in Java?

When the Java compiler encounters a string literal such as "abc", it first checks the string constant pool. If the literal is not present, the JVM creates a String object, stores it in the pool, and returns a reference to that object.

Calling new String("abc") explicitly creates a new String object on the heap, even if an identical literal already exists in the constant pool. This heap object is distinct from the pooled instance.

Therefore, the statement String s = new String("abc") can result in different numbers of objects based on the state of the constant pool:

Case 1 – Literal not in the pool:

A String object is created in the constant pool.

A separate String object is created on the heap by new String("abc").

Total objects created: two.

Case 2 – Literal already in the pool:

Only the heap object created by new String("abc") is added.

Total objects created: one.

Example code demonstrating the behavior:

public class StringCreationExample {
    public static void main(String[] args) {
        String s1 = "abc";                     // reference to pooled object
        String s2 = new String("abc");          // new heap object
        System.out.println(s1 == s2); // false, different objects
        System.out.println(s1.equals(s2)); // true, same content
    }
}

The program prints:

false
true

In this example, s1 refers to the constant‑pool instance, while s2 refers to the newly allocated heap instance. Consequently, the expression s1 == s2 evaluates to false because the two references point to distinct objects.

If the constant pool already contains the literal, only the heap object is created, resulting in a single new object.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaObject CreationstringConstant PoolReference Equality
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

0 followers
Reader feedback

How this landed with the community

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.