Java and Spring Level 2 This level · 32 of 84
Medium java java-21

How does the String pool work, and how many objects does new String create?

The 30 second answer

I'd say one or two. new always creates a new String on the heap. The literal is interned, so the first time that constant is resolved the JVM also creates a pooled instance if one is not already there. After that, only the new object is created. I use equals for content, == only when I mean the same interned instance, and intern to get the canonical pooled string.

Could you say this in an interview?

String s = new String("CJD"); creates one or two String objects, not a fixed two.

  • new String(...) always creates a new String instance on the heap.
  • The literal "CJD" refers to a pooled instance. The JLS says a string literal always refers to the same String instance because literals, and string-valued constant expressions, are interned "as if by execution of" String.intern. The JVM specification derives that instance when the CONSTANT_String run-time constant is resolved, which is what ldc then pushes. If an equal string is already interned, no second object is created; otherwise the JVM creates the pooled instance and interns it.

So the first time that literal is resolved, you typically get two objects: the pooled literal and the object new returns. Later executions in the same JVM, once "CJD" is already in the pool, create only the new object.

String a = "CJD";
String b = new String("CJD");
String c = b.intern();
System.out.println(a == b);      // false: different instances
System.out.println(a.equals(b)); // true: same characters
System.out.println(a == c);      // true: intern() returned the pooled instance

== compares references. equals compares character content. intern() returns the canonical pooled instance: if the pool already contains an equal string, that one is returned; otherwise the method adds this string to the pool and returns it. For any two strings s and t, s.intern() == t.intern() if and only if s.equals(t).

The String(String) constructor still allocates a new String object. Its Java 21 Javadoc says the new string is a copy of the argument, and that "unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable." In OpenJDK 21 the implementation shares the original's internal value array rather than copying the characters, which is safe because String is immutable. That sharing is an OpenJDK implementation detail, not the public contract.

Since JDK 7, interned strings live in the main Java heap (the young and old generations), not in the permanent generation.

Sources (6)
  1. JLS 21 §3.10.5 String Literalsdocs.oracle.com
  2. JVMS 21 §5.1 The Run-Time Constant Pooldocs.oracle.com
  3. String.intern() Javadoc (Java 21)docs.oracle.com
  4. String(String) Javadoc (Java 21)docs.oracle.com
  5. OpenJDK 21 String source (String(String) shares value)github.com
  6. Java Virtual Machine Enhancements in JDK 7: interned stringsdocs.oracle.com
Previous
Next