Probably you already know what it is, in case you don’t, please don’t stop unless you read and understand the whole thing.
String interning is a method where each distinct and immutable value of string is stored in a data structure. The data structure is called a string pool. The string interning storage method makes certain string operations time or space efficient.
String is actually the original copy of the immutable string stored in the string pool. Java as well uses the same flyweight design pattern of String Intern. If you are not familiar with fly weight pattern, you’ll find a detailed explanation in my upcoming posts.
String interning in Java
As discussed above, java keeps a string pool of all the immutable string objects it has been storing in the memory. Intern is responsible for returning this canonical value of the string saved in the string pool. The following statements are true about String#intern:
- Intern returns the canonical string from the string pool.
- a.intern() == b.intern() will only yield true, if a.equals(b) is true.
- String literals are already in canonical forms, thus intern on a literal makes no sense.
- Objects created with new String() will yield the canonical string form string pool, when interned.
- From 3, string literals are already interned.
Consider the following code-
String string1 = "test"; String string2 = "test"; String string3 = new String("test"); System.out.println(string1 == string2); System.out.println(string1.intern() == string2.intern()); System.out.println(string1.intern() == string3.intern()); System.out.println(string1.equals(string2)); System.out.println(string1.equals(string3)); System.out.println(string1 == string3);
Lets discuss what each line does-
- Line 1 creates a string with string literal “test”, this causes the string pool to be populated with the immutable string literal “test”.
- Line 2 creates another string with the same string literal, this creates a new reference, but doesn’t insert the string literal in string pool again.
- Line 3 creates a reference to a new string object, and this new string object refers to the canonical string “test” in the string pool.
- In line 5, the == equates the identity of the two operands. Since, the two operands point to the same string literal in the string pool, it equates to TRUE.
- In line 6, the intern method tries to find the canonical form. But, the strings are already in their canonical form and it returns TRUE. Please note, that when comparing two string literals, intern is not required due to fact 3 discussed above.
- Line 7, although string1 and string3 point to different objects, interning them both gives the same string literal from string pool. Thus, it return TRUE.
- Line 8, equals as we know, always returns true if the values of the strings being compared are same. Thus, it returns TRUE.
- Line 9 returns TRUE. Since, value of string1 and string3 is same.
- Line 10 returns FALSE. Why? Simply because the two strings string1 and string3 point to different objects. And == operator checks for same identity.
In case you have not understood anything, feel free to leave a query. And suggested are more than welcome.