String intern() method in Java. Why we use it?

Briefly, Java's String class has a public method intern() that returns a canonical representation for the string object. Java's String class privately maintains a pool of strings, where String literals are automatically interned. When the intern() method is invoked on a String object it looks the string contained by this String object in the pool, if the string is found there 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.

The pool of strings in Java is maintained for saving space and for faster comparisons. Two string literals can be compared by == operator which is faster than equals(), while two String objects cannot be compared by == operator. You should use equals() method to compare two String objects.

The intern() method helps in comparing two String objects with == operator by looking into the pre-existing pool of string literals, no doubt it is faster than equals() method. Normally Java programmers are advised to use equals(), not ==, to compare two strings. This is because == operator compares memory locations, while equals() method compares the content stored in two objects. Java's intern() method provides us an opportunity to intern strings when they are not constants and either objects or created at run time, and we want to quickly compare them to other interned strings. Of course, this will save time if we make lot of such comparisons in our program.

Remember that we only need to intern strings when they are not constants, and we want to be able to quickly compare them to other interned strings. The intern() method should be used on strings constructed with new String() in order to compare them by == operator. Let's take a look at the following Java program, and you will understand when the intern() method is proven to be useful.

/* InternDemo.java: Demonstrating intern method*/
public class InternDemo
{
  public static void main(String[] args)
  {
    String s1 = "Hello";
	String s2 = s1;
	String s3 = new String("Hello");
	String s4 = "lo";
 
	System.out.println(s1 == "Hello"); //true
	System.out.println(s1 == s2); //true
	System.out.println(s1 == s3); //false
 
	//Strings computed by concatenation at 
	//run-time are newly created and therefore distinct.
	System.out.println("Hello" == "Hel"+s4); //flase
 
	//s3 is not literal, so distinct
	System.out.println(s3 == ("Hel"+s4).intern()); //false
 
	//The result of explicitly interning a computed string is the same 
	//string as any pre-existing literal string with the same contents.
	System.out.println(s1 == s3.intern()); //true
	System.out.println("Hello" == ("Hel"+s4).intern()); //true
	System.out.println(s1 == ("Hel"+s4).intern()); //true
	System.out.println(s3.intern() == ("Hel"+s4).intern()); //true
  }
}
 
OUTPUT
======
D:\JavaPrograms>javac InternDemo.java
D:\JavaPrograms>java InternDemo
true
true
false
false
false
true
true
true
true

Above program demonstrates that strings created at run time are not interned by default, and we have to invoke intern() method to compare them by == operator.

Moreover, look at the following points mentioned in Java Language Specification about literal strings:

  • Literal strings within the same class in the same package represent references to the same String object.
  • Literal strings within different classes in the same package represent references to the same String object.
  • Literal strings within different classes in different packages likewise represent references to the same String object.
  • Strings computed by constant expressions are computed at compile time and then treated as if they were literals.
  • Strings computed by concatenation at run-time are newly created and therefore distinct.
  • The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.

Hope you have enjoyed reading string interning in Java. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.