String Literal vs New String Object
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
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));
}
}true
false
false
trueHow it works
s1ands2come from the same literal, so both point to one pooled object.- Each
new String("Java")builds its own object outside the pool, sos3ands4are two different objects, and neither is the pooled one. - All four hold the same characters, so
equalsistruefor every pair.
How Many Objects?
| Statement | New 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:
| Constructor | Real 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
- Easy: Predict the output of
"Pune" == "Pune"andnew String("Pune") == "Pune", then check. - Harder: Read a city name with
Scannerand compare it with the literal"Pune"using both==andequals. Explain why the results differ.
Key Takeaways
Was this tutorial helpful?