String Intern

 

String#intern

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

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:

  1. Intern returns the canonical string from the string pool.
  2. a.intern() == b.intern() will only yield true, if a.equals(b) is true.
  3. String literals are already in canonical forms, thus intern on a literal makes no sense.
  4. Objects created with new String() will yield the canonical string form string pool, when interned.
  5. 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-

  1. Line 1 creates a string with string literal “test”, this causes the string pool to be populated with the immutable string literal “test”.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. Line 8, equals as we know, always returns true if the values of the strings being compared are same. Thus, it returns TRUE.
  8. Line 9 returns TRUE. Since, value of string1 and string3 is same.
  9. 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. :-)

About these ads

About Gaurav

I'm Gaurav; friends call me Teddy, n i'm shivi fr my family!! A java enthusiast who likes experimenting with what he knows. View all posts by Gaurav

4 responses to “String Intern

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 74 other followers

%d bloggers like this: