Java: String intern() method

In this article, we will discuss intern() method of String class which is used to return interned string

intern() method:

  • This String method is used to return canonical representation of String object

Method Signature:

public String intern();

Returns:

  • Returns a canonical representation for the string object

 

Background about String storage area:

There are basically 2 areas where we can store string

String stored at constant pool are referred as String Literal whereas string object created using new operator stored at heap memory

 

Why there is a need for 2 separate areas for Strings ?

  • Generally, everything in Java is object
  • So, keeping in line with that string is also object (which can be created using new operator)
  • At the same time, there is another storage area called String literal pool which maintains list of unique string values/contents thereby reducing space for string storage of same contents/values
  • String constant pool is much faster when comparing with string object for operations like retrieval or string comparison
  • And string is the most frequently used object in Java for sending & receiving information across network connection
  • So to attain the feature of faster access/retrieval & comparison, Java has a separate arrangement for string object which can be stored inside String Literal pool

Why there is an intern() method in String class ?

  • Assume that, if there are numerous String objects and it is frequently accessed and made comparison with some other string object
  • In this case, instead of comparing with equals() method, we can simply use “==” operator after interning the string
  • So, for faster accessibility and comparison of any string object interned string can be used
  • This is the main reason for introducing intern() method inside String class

What happens exactly, when intern() method is invoked on any String object ?

When string object is interned, then

  • It checks whether string object’s content already present inside String Literal pool
  • If it is present, then simply reference is returned
  • Otherwise, a new string is added to the string literal and its reference is returned
  • For better understanding, go through below examples
  • Note: all string inside string constant pool are self-interned

 

Examples on intern() method:

String literal is compared with String object after interning

In the below example,

  • define a simple string literal with value “Bench”
  • now, again define a string object using new operator
  • Compare 1: compare string literal with string object of same content which returns false because of different references
  • Compare 2: After this, intern the string object and compare string literal with interned string which returns true because both refers to same string (or both pointing to same string with same reference)
  • Note: equals() method compares string contents whereas “==” operator checks references of both strings

StringInternMethod.java

package in.bench.resources.string.methods;

public class StringInternMethod {

	public static void main(String[] args) {

		// string literal
		String str1 = "Bench";

		// string object using new operator
		String str2 = new String("Bench");

		// compare 1: string literal with string object
		if(str1 == str2) {
			System.out.println("Compare 1: String literal and "
					+ "String object are same");
		}
		else {
			System.out.println("Compare 1: String literal and "
					+ "String object are NOT same");
		}

		// intern the string object using intern() method
		// this create string object in String Constant pool
		String str3 = str2.intern(); 

		// compare 2: interned string object with string literal
		if(str1 == str3) {
			System.out.println("\nCompare 2: String literal and "
					+ "interned String object are same");
		}
		else {
			System.out.println("\nCompare 2: String literal and "
					+ "interned String object are NOT same");
		}
	}
}

Output:

Compare 1: String literal and String object are NOT same

Compare 2: String literal and interned String object are same

 

For more read String Literal and String constant pool concept

 

References:

 

Happy Coding !!
Happy Learning !!

Java: String isEmpty() method
Various ways to split String in Java – 3 ways