Fundamentals 12 min read

Why String.intern Makes Different Strings Equal – A Deep Dive into Java’s String Pool

This article explains how Java's String.intern method interacts with the string constant pool, detailing the differences between JDK6, JDK7, and JDK8 implementations, and walks through multiple code examples that show why strings that were originally unequal become identical after interning.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
Why String.intern Makes Different Strings Equal – A Deep Dive into Java’s String Pool

Background

The article is part of a series that explores Java interview questions. It focuses on the behavior of String.intern() and how it affects string comparison, using the HotSpot JVM as the reference implementation.

Purpose of intern()

The intern() method works as follows:

If the string content already exists in the string constant pool (i.e., equals() returns true), the method returns the existing pool reference.

If the content does not exist, a new entry is created in the pool that points to the heap string, and that reference is returned.

In short, intern() ensures that a string is represented by a single canonical instance in the pool.

String Constant Pool Implementation

In HotSpot, the pool is managed by the StringTable class, a hash table with a default size of 1009. There is only one instance per JVM, shared by all classes.

JDK version differences:

JDK6 and earlier: The pool resides in the PermGen area of the method space. The fixed size can cause hash collisions and severe performance degradation when many strings are stored, often leading to OutOfMemoryError.

JDK7: The pool was moved to the heap, making it easier to tune by adjusting heap size. The pool can now store references to heap strings, not just literal constants.

JDK8: The pool remains on the heap with the same reference‑storage capability.

String Pooling Process – Examples

All examples are based on JDK8.

Literal Assignment

String wechat = "程序新视界";

The literal is stored directly in the constant pool. Declaring the same literal again returns the same reference: String wechat1 = "程序新视界"; Both wechat and wechat1 point to the same pool entry, so wechat == wechat1 is true.

Creating a String with new

String wechat2 = new String("程序新视界");

If the pool already contains the literal, the heap object’s reference is set to the existing pool entry, but wechat2 itself resides on the heap, so wechat == wechat2 is false while wechat.equals(wechat2) is true.

Concatenation with +

String s1 = "程序";
String wechat3 = new String(s1 + "新视界");

The concatenation is compiled to a StringBuilder operation, creating a new heap string without adding a literal to the pool.

Interning a Heap String

String wechat3 = new String(s1 + "新视界");
wechat3 = wechat3.intern();

After calling intern(), the pool now contains a reference to the heap string. Subsequent literals like String wechat5 = "程序新视界"; will resolve to the same reference, making wechat3 == wechat5 true.

When the Pool Lacks the String

String s2 = "关注";
String wechat4 = new String(s2 + "公众号");
wechat4 = wechat4.intern();

Because the pool does not have "关注公众号" initially, intern() adds a reference to the heap string itself. The returned reference is the heap address, so the variable’s value does not change.

Analysis of the Original Interview Code

String s1 = new String("he") + new String("llo");
String s2 = new String("h") + new String("ello");
String s3 = s1.intern();
String s4 = s2.intern();
System.out.println(s1 == s3); // true
System.out.println(s1 == s4); // true

Both s1 and s2 create heap strings "hello". The first intern() call stores s1 in the pool and returns its own reference, so s1 == s3 is true. The second call finds the same literal already in the pool (pointing to s1), so s2.intern() returns the reference to s1, making s1 == s4 true as well.

Conclusion

The key takeaway is that intern() returns the pool reference when the string exists; otherwise it adds the heap reference to the pool and returns that heap address. Understanding this behavior clarifies why strings that were originally unequal become equal after interning, and it equips readers to answer related interview questions accurately.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJVMMemory ManagementStringinternString pool
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.