Java String intern() Method

This post gives an insight into the Java String intern() method which is closely associated with the concept of “interning” strings in Java and maintaining a String pool in Java.

String interning in Java

In Java, string literals are “interned” so as to share unique instances. A pool of strings, initially empty, is maintained privately by the class String. For every string literal (String value enclosed in double quotes) JVM scans the String pool for any String having the same value, if found same reference is returned. So, new memory is not allocated for the same string, existing memory is used instead and the reference is shared.

Let’s understand with an example. if two Strings are created as follows-

String str1 = “Hello”
String str2 = “Hello”

Then the same memory is referenced by both the objects.
String interning in Java

intern method in String

String literal are interned by default in Java but same is not true for the Strings created using new operator. For such String instances memory is allocated on the heap rather than in the String pool.

For example if four Strings are created as follows-

String str1 = “Hello”;
String str2 = “Hello”;
String str3 = new String(“Hello”);
String str4 = new String(“Hello”);

str1 and str2 share the same reference in the constant string pool where as str3 and str4 have separate references to a memory location on the heap.

Java String pool

Even for Strings created using new operator you can get String object from the pool (if same value already exists) using intern() method in Java String.

  • public String intern()– Returns a canonical representation for the string object. When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

Java intern() method examples

Let’s try to understand string interning with the help of few examples.

public class StringInterning {
  public static void main(String[] args) {
    // Stored in String pool
    String str1 = "Hello";
    // Stored in String pool
    String str2 = "Hello";
    // Stored in heap
    String str3 = new String("Hello");
    // returns true
    if(str1 == str2) {
      System.out.println("str1 and str2 are pointing to same memory reference");
    }else{
      System.out.println("str1 and str2 are not pointing to same memory reference");
    }		
    // returns false
    if(str1 == str3) {
      System.out.println("str1 and str3 are pointing to same memory reference");
    }else{
      System.out.println("str1 and str3 are not pointing to same memory reference");
    }
    // shares the reference in String pool
    String str4 = str3.intern();
    // returns true
    if(str1 == str4) {
      System.out.println("str1 and str4 are pointing to same memory reference");
    }else{
      System.out.println("str1 and str4 are not pointing to same memory reference");
    }
  }
}

Output

str1 and str2 are pointing to same memory reference
str1 and str3 are not pointing to same memory reference
str1 and str4 are pointing to same memory reference

In the example Strings str1 and str2 are created as String literals and have the same value so these two objects share the same reference in the String pool. That is why comparing them using equality operator (‘==’) returns true.

String str3 is created using the new operator so memory for it is allocated in heap. Even if str3 has the same value as str1 and str2 but it doesn’t share the same reference. That is why comparing String str3 with str1 or str2 using equality operator (‘==’) returns false.

String str4 is created using intern method so it looks into String pool if such a value already exists. Since value “Hello” is already in the pool so str4 shares reference with str1 and str2. That is why comparing str4 with str1 or str2 using equality operator (‘==’) returns true.

Here is another Java program using intern() method.

public class StringInterning {
  public static void main(String[] args) {
    // Stored in heap
    String str1 = new String("Hello");
    // Stored in heap
    String str2 = new String("Hello");
    // returns false
    if(str1 == str2) {
      System.out.println("str1 and str2 are pointing to same memory reference");
    }else{
      System.out.println("str1 and str2 are not pointing to same memory reference");
    }
    String str3 = str1.intern();
    // returns false
    if(str1 == str3) {
      System.out.println("str1 and str3 are pointing to same memory reference");
    }else{
      System.out.println("str1 and str3 are not pointing to same memory reference");
    }
    // store in the pool
    String str4 = "Hello";
    // returns true
    if(str3 == str4) {
      System.out.println("str3 and str4 are pointing to same memory reference");
    }else{
      System.out.println("str3 and str4 are not pointing to same memory reference");
    }
  }
}

Output

str1 and str2 are not pointing to same memory reference
str1 and str3 are not pointing to same memory reference
str3 and str4 are pointing to same memory reference

In the example Strings str1 and str2 are created using new operator so two separate objects are created. That is why comparing them using equality operator (‘==’) returns false.

String str3 is created using intern() method so memory for str3 is allocated in the pool. That is why comparing str3 with either str1 or str2 using equality operator (‘==’) returns false.

String str4 is created as String literal so string pool is checked to see if such a value already exists. Same value is already there so str4 has the same reference as str3. That is why comparing str3 with str4 using equality operator (‘==’) returns true.

Related Posts

That’s all for the topic Java String intern() Method. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.