Java string intern()

In this article, we will take a deep dive into string intern() method in java with explanation and example programs.

intern() method in a java string is used to return the canonical representation of a string.
When a string is interned, a copy of the string is stored in the string pool, and the reference to this copy is returned. If the string already exists in the string pool, then the existing reference is returned.

Java docs for intern() method state,

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.

What is canonical representation
In Java, the “canonical representation” of a string refers to the specific, standardized way that the string is stored in memory.
The JVM uses this representation to ensure that two different strings with the same characters are considered equal, even if they were created using different methods or at different times.
The canonical representation is also used to optimize memory usage by ensuring that multiple references to the same string are stored in the string constant pool, rather than creating multiple copies of the string.

For example, when a String object is created using the new keyword and the string literals with same value are created using double quotes, both the references point to different memory location but they have the same value, so JVM will consider them as equal by comparing their canonical representation.
What is String pool
String constant pool is a special area of memory where strings that are used as constants are stored.

string constant pool
When a string is created using double quotes, the JVM first checks the string constant pool to see if an identical string already exists.
If it does, a reference to the existing string is returned, rather than creating a new string object on the heap.
If an identical string is not found in the string constant pool, a new string object is created on the heap and a reference to it is added to the string constant pool.
This helps to conserve memory by reducing the number of duplicate strings in the system.
However, if a String object is created using the new keyword, a new object is created in the heap, even if a String with the same value already exists in the String constant pool.
So, the String constant pool is used to store the string literals and heap memory is used to store the String objects.
intern() example
Below is an example of intern() method in java.

String str1 = new String("codippa");
String str2 = str1.intern();
String str3 = "codippa";

System.out.println(str1 == str2); // false
System.out.println(str2 == str3); // true

In this example, str1 is created using the new keyword, which creates a new object in heap memory.
The intern() method is called on str1, which adds a copy of the string “codippa” to the string pool and returns a reference to this copy.
str2 is assigned the reference returned by the intern() method. str3 is assigned a reference to the string “codippa” that already exists in the string pool.

String intern() example 2
Below is another example of string intern() method.

String s1 = new String("codippa");
String s2 = new String("codippa");
String s3 = "codippa";
String s4 = "codippa";

System.out.println(s1 == s2); // false
System.out.println(s1.intern() == s2.intern()); // true
System.out.println(s3 == s1.intern()); // true
System.out.println(s3 == s2.intern()); // true
System.out.println(s3 == s4); // true

In this example, we are creating two new String objects s1 and s2 and assigning the value “codippa” to both of them.
Then we are creating two string literals s3 and s4 and assigning the value “codippa” to both of them.

Here, s1 and s2 are two different objects with the same value “codippa” in heap memory, so s1 == s2 will return false.

When we call s1.intern() and s2.intern(), the method will return the reference to the same object of the string “codippa” that is in the string pool.
Therefore, s1.intern() == s2.intern() will return true.

s3 and s4 are string literals, which are already in the string pool, so s3 == s4 will return true.

s3 is in the string pool, s1.intern() will return the reference to the same object, so s3 == s1.intern() will return true.

s3 is in the string pool, s2.intern() will return the reference to the same object, so s3 == s2.intern() will return true.

In this example, intern() method is used to find the existing reference of a string from string pool.
If the string already exists in the string pool then its reference is returned, otherwise a new string object in the string pool is created and reference of that object is returned.

Hope the article was useful.