String Literal vs New String Object

Naveen AnamaneniUpdated · 4 min read

There are two ways to write the same text: String s = "Java"; and String s = new String("Java");. They print exactly the same thing, so why does it matter? Because they create a different number of objects in different places, and that changes how == behaves and how much memory your program uses. It's also one of the most common Java interview questions.

Side by Side

Literals share one pooled object; new always makes another
LiteralVsNew.java▶ Try it
public class LiteralVsNew {
    public static void main(String[] args) {
        String s1 = "Java";
        String s2 = "Java";
        String s3 = new String("Java");
        String s4 = new String("Java");
 
        System.out.println(s1 == s2);
        System.out.println(s3 == s4);
        System.out.println(s1 == s3);
        System.out.println(s1.equals(s3) && s3.equals(s4));
    }
}
Output
true
false
false
true

How it works

  1. s1 and s2 come from the same literal, so both point to one pooled object.
  2. Each new String("Java") builds its own object outside the pool, so s3 and s4 are two different objects, and neither is the pooled one.
  3. All four hold the same characters, so equals is true for every pair.

How Many Objects?

StatementNew objects created
String a = "Java"; (first time this text appears)1, in the pool
String b = "Java"; (the text is already pooled)0: reuses the pooled one
String c = new String("Java");1 on the heap, plus 1 in the pool if "Java" wasn't already there

That last row is the classic interview answer: new String("Java") can create two objects. The literal "Java" inside the parentheses goes to the pool (if it isn't there yet), and new makes another copy outside it.

Why Java Allows new String at All

new String(...) exists for a few real jobs, none of which involve copying a literal:

ConstructorReal use
new String(char[] chars)Build text from a character array
new String(byte[] bytes, StandardCharsets.UTF_8)Turn bytes from a file or network into text (see "Character Encoding and Charsets in Java")
new String(StringBuilder sb)Rarely needed; sb.toString() is clearer

Common Mistakes

Writing new String("...") around literals. It wastes memory and makes == even less reliable.

Testing string equality with == because "it worked with literals". Text from input, files or + at runtime isn't pooled.

Thinking new String makes a "safer copy". Strings are immutable; sharing is already safe.

Interview Corner

What is the difference between String s = "Java" and String s = new String("Java")?

The literal form uses the String pool: if "Java" already exists there, s points to that shared object, otherwise one pooled object is created. new String("Java") always creates a new object on the heap outside the pool, and the literal argument may also create a pooled object, so it can create two objects. Both hold the same characters, so equals is true, but == between them is false.

How many objects does new String("Java") create?

Up to two: one in the String pool for the literal "Java", if it isn't already there, and one regular heap object created by new. If the literal is already pooled, it creates just one.

Practice

  1. Easy: Predict the output of "Pune" == "Pune" and new String("Pune") == "Pune", then check.
  2. Harder: Read a city name with Scanner and compare it with the literal "Pune" using both == and equals. Explain why the results differ.

Quick check: Literal vs new

4 questions
1String a = "Hi"; String b = new String("Hi"); What is a == b?
2How many objects can new String("Hi") create if "Hi" isn't pooled yet?
3Which is the recommended way to create text?
4String a = "Hi"; String b = "Hi"; How many objects?

Key Takeaways

Was this tutorial helpful?

Lesson 188 of 482