In Java, every string literal such as "java" lives in a shared string pool, so equal literals refer to the same object. intern() returns the pooled copy of a string's text, adding it to the pool first if it is not there yet.
After interning, two strings with equal content are the same object, so a.intern() == b.intern() is true exactly when a.equals(b). Ordinary code should still compare with equals; intern() mainly explains why == sometimes appears to work.
intern() is an instance method of java.lang.String, available since JDK 1.0. Call it on a string, as in text.intern(); calling it on a null reference throws NullPointerException. It only reads the text; the string itself is never changed.
Syntax
Call it with no arguments and use the returned reference.
java
String canonical = someText.intern();
boolean same = canonical == "java"; // true when the text is "java"
Call
What it does
"java".intern() == "java"
true: literals are already pooled.
new String("java").intern() == "java"
true.
new String("java") == "java"
false: a separate object.
intern() does not change the string it is called on; it returns a reference. new String("java") remains a separate object even after intern() was called on it.
Library declarations
java
public String intern();
Parameters
Parameter
Description
None
intern() takes no parameters.
Return Value
Return Value
Description
String
The pooled string with the same content: the existing pool entry, or this string after it was added.
Java Examples
Example 1 — Literals, new String, and intern()
Compare references with == and content with equals.
java
public class InternLiterals {
public static void main(String[] args) {
String a = "java";
String b = "java";
String c = new String("java");
System.out.println("a == b: " + (a == b));
System.out.println("a == c: " + (a == c));
System.out.println("a == c.intern(): " + (a == c.intern()));
System.out.println("a.equals(c): " + a.equals(c));
}
}
Output
a == b: true
a == c: false
a == c.intern(): true
a.equals(c): true
How It Works
Pooled literals.a and b both point at the single pooled "java", so == is true.
new makes a copy.new String("java") always creates a fresh object, so a == c is false although the text matches.
intern() returns the pool entry.c.intern() gives back the same object as a, while equals was true all along.
Example 2 — Strings Built at Runtime Are New Objects
Concatenating a variable creates a new string; interning maps it back to the pool.
java
public class InternRuntime {
public static void main(String[] args) {
String part = "ja";
String built = part + "va";
System.out.println("built == \"java\": " + (built == "java"));
System.out.println("built.intern() == \"java\": " + (built.intern() == "java"));
System.out.println("built.equals(\"java\"): " + built.equals("java"));
}
}
Output
built == "java": false
built.intern() == "java": true
built.equals("java"): true
How It Works
Concatenation at runtime.part is an ordinary variable, so part + "va" is evaluated while the program runs and produces a new object.
Look it up in the pool.built.intern() returns the pooled "java", the same object the literal refers to.
equals never cared. Comparing content gives true whether or not the strings were interned.
Example 3 — final Constants Are Pooled Automatically
A final local initialized with a literal is a compile-time constant, and so is its concatenation.
final + literal: true
variable + literal: false
after intern(): true
How It Works
Folded by the compiler.fixed is a constant variable, so fixed + "va" is computed at compile time and stored as the pooled literal "java".
Without final, no folding.changing is a normal variable, so the concatenation happens at runtime and creates a separate object.
Back to one object.runtime.intern() returns the pooled string, which is the same object as constant.
Frequently Asked Questions
It returns the canonical copy of the string from the JVM string pool, adding the string to the pool if no equal string is there yet.
A JVM-wide table of unique strings. All string literals and compile-time constant strings are stored there, so equal literals share one object.
When both sides are pooled (literals, constants, or interned strings) they are the same object. Strings built at runtime usually are not, so always use equals().
No. It returns a reference to the pooled string; the object you called it on stays as it was.
Only in special cases with many duplicate strings, confirmed by profiling. For normal comparisons, equals() is correct and simpler.