The Java String intern() return the reference of a equal string literal present in string pool. If there is an existing string literal present in string pool then it’s reference is returned. Otherwise a new string with same content is created and the reference of new string is returned.
String equality is checked with String.equals() method.
1. String pool
String pool is a reserved memory area in heap memory which Java uses for storing string constants. Note that Java strings are immutable by default.
Java stores only one copy of every distinct String value in the string pool. It helps to reuse String objects to save memory during program execution. There may be many references to a string in running program, but there will be only copy of string inside string pool.
1.1. Two ways to create string
In Java we can create strings in two ways.
String str1 = new String("hello world"); String str2 = "hello world";
In above example, both ways are used to create strings, but later is recommended which uses string literals. String literals always go to string pool.
When we create string with new keyword, two objects will be created i.e. one in the Heap Area and another in the String constant pool. The created string object reference always points to heap area object.
To get the reference of same object created in string pool, use intern()
method.
2. Java String intern() method
The String.intern()
returns a reference to equal string literal present in string pool.
As we know that all string literals are automatically created in String pool, so intern() method is applicable to String objects created via 'new'
keyword.
String intern()
is native method. By the help of intern()
method, we can get the reference of corresponding String constant pool object of an original string object.
3. Java String intern() example
Java program intern a string with String.intern() method.
public class StringExample { public static void main(String[] args) { //String object in heap String str1 = new String("hello world"); //String literal in pool String str2 = "hello world"; //String literal in pool String str3 = "hello world"; //String object interned to literal //It will refer to existing string literal String str4 = str1.intern(); System.out.println(str1 == str2); //false System.out.println(str2 == str3); //true System.out.println(str2 == str4); //true } }
Program output.
false true true
In this example, we learned to intern a string in Java. This is native method and provides very high performance.
References:
A Guide to Java String
String Java Doc
Jim Forsyth
Is it really true that “When we create string with new keyword, two objects will be created i.e. one in the Heap Area and another in the String constant pool.”
There would seem to be little need for an exposed intern() method if ‘new’ also creates a pooled object.
Lokesh Gupta
That’s true.
intern()
is a legacy method. And for the same reason, you will not see its use anywhere (probably few places in JDK).Namrata Maradkar
But we can get a reference from String pool by using String literal.. we can so String str = “hello world”; this will return reference from pool. Why do we even need intern() method to get literal String ?
Lokesh Gupta
Probably, its the reason we never see its use in real-world applications.