Sub topic 15
String Pool, Interning, and Identity
Java can share some strings instead of creating a separate string for each use. This sharing can save memory. But it also makes one common mistake harder to spot: using == to compare string contents.
What is the string pool?
A string object is a piece of data that holds text. A reference is a value that points to an object.
The string pool is a group of shared string objects managed by Java. A string literal is text written between double quotes in your code, such as "cat". When two literals have the same contents, they refer to the same pooled string.
This sharing is safe because strings are immutable: once a string object is created, its text cannot change.
Identity is not the same as content
Object identity means whether two references point to the very same object. For string references, == checks identity. The equals method checks whether the strings have the same contents.
Save this example as StringPoolDemo.java:
class StringPoolDemo {
public static void main(String[] args) {
String first = "cat";
String second = "cat";
String copy = new String("cat");
String shared = copy.intern();
System.out.println("Literals share an object: " + (first == second));
System.out.println("Copy is the same object: " + (first == copy));
System.out.println("Copy has the same text: " + first.equals(copy));
System.out.println("Interned string is shared: " + (first == shared));
}
}Compile and run it:
javac StringPoolDemo.java
java StringPoolDemoThe output is:
Literals share an object: true
Copy is the same object: false
Copy has the same text: true
Interned string is shared: trueThe two literals refer to the same pooled object. The new String expression creates a separate object, even though it holds the same text. That is why first == copy is false, but first.equals(copy) is true.
What does `intern()` do?
Interning means finding or adding a string in the pool. The intern() method returns the pooled string with the same contents as the string you call it on.
If the pool already has an equal string, intern() returns that object. Otherwise, it adds the string to the pool and returns it.
In the example, "cat" is already in the pool. So copy.intern() returns the pooled object, which is also the object referenced by first. It does not change copy; copy still points to the separate object.
When should you use it?
For normal content checks, use equals, not ==. Code tested only with literals may seem to work with ==, then fail when the same text comes from a user or a file.
Use intern() only when you have a reason to share many repeated strings. Interning also has a cost, and it does not guarantee that your program will use less memory or run faster.
Exercise
If String a = "sun"; and String b = new String("sun");, what do a == b and a.equals(b) return?
Show answer
a == b returns false because they are different objects. a.equals(b) returns true because their contents match.
Exercise
After String c = b.intern(); in the previous exercise, is a == c true?
Show answer
Yes. The literal "sun" is in the pool, so intern() returns that pooled object.