Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should we use intern method of String on String literals

According to String#intern(), intern method is supposed to return the String from the String pool if the String is found in String pool, otherwise a new string object will be added in String pool and the reference of this String is returned.

So i tried this:

String s1 = "Rakesh"; String s2 = "Rakesh"; String s3 = "Rakesh".intern();  if ( s1 == s2 ){     System.out.println("s1 and s2 are same");  // 1. }  if ( s1 == s3 ){     System.out.println("s1 and s3 are same" );  // 2. } 

I was expecting that s1 and s3 are same will be printed as s3 is interned, and s1 and s2 are same will not be printed. But the result is: both lines are printed. So that means, by default String constants are interned. But if it is so, then why do we need the intern method? In other words when should we use this method?

like image 211
Rakesh Juyal Avatar asked Dec 06 '09 11:12

Rakesh Juyal


People also ask

What is String intern () method used for?

The method intern() creates an exact copy of a String object in the heap memory and stores it in the String constant pool. Note that, if another String with the same contents exists in the String constant pool, then a new object won't be created and the new reference will point to the other String.

Why is it beneficial for Java programs that string literals are interned?

String Interning is a method of storing only one copy of each distinct String Value, which must be immutable. By applying String. intern() on a couple of strings will ensure that all strings having the same contents share the same memory.

What is the use of the intern () method A It returns the existing string from memory B it creates a new string in the database C it modifies the existing string in the database?

Java String intern() The Java String class intern() method returns the interned string. It returns the canonical representation of string. It can be used to return string from memory if it is created by a new keyword.

Are string literals interned?

All literal strings and string-valued constant expressions are interned.


1 Answers

Java automatically interns String literals. This means that in many cases, the == operator appears to work for Strings in the same way that it does for ints or other primitive values.

Since interning is automatic for String literals, the intern() method is to be used on Strings constructed with new String()

Using your example:

String s1 = "Rakesh"; String s2 = "Rakesh"; String s3 = "Rakesh".intern(); String s4 = new String("Rakesh"); String s5 = new String("Rakesh").intern();  if ( s1 == s2 ){     System.out.println("s1 and s2 are same");  // 1. }  if ( s1 == s3 ){     System.out.println("s1 and s3 are same" );  // 2. }  if ( s1 == s4 ){     System.out.println("s1 and s4 are same" );  // 3. }  if ( s1 == s5 ){     System.out.println("s1 and s5 are same" );  // 4. } 

will return:

s1 and s2 are same s1 and s3 are same s1 and s5 are same 

In all the cases besides of s4 variable, a value for which was explicitly created using new operator and where intern method was not used on it's result, it is a single immutable instance that's being returned JVM's string constant pool.

Refer to JavaTechniques "String Equality and Interning" for more information.

like image 153
Filipe Miguel Fonseca Avatar answered Sep 28 '22 11:09

Filipe Miguel Fonseca