How Java’s String.intern() Boosts Memory Efficiency and Performance

Java’s String.intern() method adds a string to the constant pool, enabling memory sharing and faster reference comparisons; this article explains its behavior, demonstrates usage with code examples, and discusses performance benefits and cautions for optimal use in real‑world applications.

Xuanwu Backend Tech Stack
Xuanwu Backend Tech Stack
Xuanwu Backend Tech Stack
How Java’s String.intern() Boosts Memory Efficiency and Performance

In Java, the intern() method of the String class adds the current string object to the string constant pool (if it is not already present) and returns the reference from the pool.

Memory Optimization and Sharing

String s1 = new String("hello");
String s2 = s1.intern();
String s3 = "hello";
// s2 and s3 now reference the same "hello" in the constant pool, while s1 remains on the heap
System.out.println(s1 == s2); // false
System.out.println(s2 == s3); // true

Here, s1 is created with new String(), allocating a new object on the heap. Calling s1.intern() returns the reference from the constant pool, which is the same reference used by the literal s3. Therefore s2 == s3 is true, while s1 == s2 is false.

When intern() is invoked, if the string already exists in the constant pool, the existing reference is returned; otherwise the string is added to the pool and that reference is returned. This reduces memory usage by ensuring only one copy of identical strings is stored.

Performance Optimization

public class InternTest {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        String str1 = new String("xuanwu");
        String str2 = new String("xuanwu");
        map.put(str1.intern(), 1);
        // str2.intern() finds the same pool object, no new key is created
        System.out.println(map.containsKey(str2.intern())); // true
    }
}

In this example, both str1.intern() and str2.intern() return the reference to the same pooled string "xuanwu", so using str2.intern() to look up the map finds the existing key.

In scenarios with many duplicate strings, using intern() can improve performance by reducing memory consumption and speeding up string comparisons, because reference equality checks are faster than content comparisons.

Cautions

Although intern() can optimize memory and performance in some cases, overusing it may cause problems. The constant pool has limited space; adding a large number of long strings can lead to memory overflow, especially when interning many large strings.

For strings used only once, interning may add unnecessary overhead due to the pool lookup and possible insertion.

Overall, String.intern() is a useful tool for specific scenarios, but it should be applied judiciously based on actual needs to avoid performance or memory issues.

JavaMemory Optimizationstringintern
Xuanwu Backend Tech Stack
Written by

Xuanwu Backend Tech Stack

Primarily covers fundamental Java concepts, mainstream frameworks, deep dives into underlying principles, and JVM internals.

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.