While developing the String related program I came across with this specific function which is I think one of the most unused functionality of String in day to day development.
intern method returns canonical representation of the String by finding the match from String pool with equal method.
Before going in to the deep discussion of the intern method of String we need to peek in to some other terms. Let’s consider String Pool first,
String pool is a memory area of Heap where all the Strings are located by the java virtual machine
In the String pool there is an another small portion of memory to store String constants or literals. So String constant pool is subset of String pool in the Heap space. Consider following String declarations,
String str1 = "Hello";//In String constant pool String str2 = new String("Hello");//In String pool
Let’s consider following two cases to get more detail about the behavior of String,
String cPoolStr1 = "Hello"; String cPoolStr2 = "Hello";
Here note one thing that whenever you create String literal it will be created in String constant pool and now if you create another String with the same data it will also refer to the same String of the constant pool and Java Virtual Machine will not create new one.
String sPoolStr1 = new String("Hello"); String sPoolStr2 = new String("Hello");
Now with above String declarations which is created with new keyword are actually String Objects are created in String pool and not in String constant pool. So ultimately cPoolStr1 and cPoolStr2 will refer to the same memory address and sPoolStr1 and sPoolStr2 will refer to the two different memory addresses.
Consider following image for more,
As you can see new String Objects are created with the same content while the constants will refer to the same memory address. So in this kind of situation String pool will have same String content for multiple String Objects. As we know the cost of one character is at least 2 bytes this becomes crucial for String manipulation in huge projects where every byte matters. Solution is intern method as you already have guessed,consider the following program.
public class StringIntern { public static void main(String[] args) { String str1 = "ImproveJava";//Already interned String str2 = new String("ImproveJava"); if(str1 == str2) { System.out.println("Don't expect this"); } str2 = str2.intern(); if(str1 == str2) { System.out.println("str1 and str2 are same!!"); } } }
As you can see here str2 is interned means String class will find the matching String from the String pool with equals method and now str2 refers to the str1’s memory location and will not be a newly created “ImproveJava” in the String pool.
intern method returns the existing String from the pool which is equal (refer to the equal method) to the current String but note here that String literals are already interned as literals with same String data will refer to the same memory address.
One simple statement of Java Doc about intern method,
It follows that for any two strings
s
andt
,s.intern() == t.intern()
istrue
if and only ifs.equals(t)
istrue
.