1

I came across below behavior from intern() method , confused, any thoughts?

Case 1:

 String test3=new String("hello");  
 test3.intern();   
 String test4=new String("hello");   
 test4.intern();  
 if(test3==test4){   
    System.out.println("same obj refered for test3 and test4 ");  
 }  
 else{   
    System.out.println("new obj created for test4");   
 }  

Output:

new obj created for test4

Case 2:

String test3=new String("hello").intern();   
//test3.intern();   
String test4=new String("hello").intern();   
//test4.intern();       
if(test3==test4){   
   System.out.println("same obj referred for test3 and test4 ");    
}
else{   
   System.out.println("new obj created for test4");    
}    

Output:

same obj referred for test3 and test4
C Snover
  • 17,908
  • 5
  • 29
  • 39
vivek shetty
  • 79
  • 1
  • 1
  • 4

2 Answers2

4

In your CASE1 you ignore the return value of intern. Thats why the variables still contain references to the original, not interned, string objects.

To understand whats going on consider the following code

    String test1 = "hello";
    String test2 = new String(test1);
    String test3 = test2.intern();
    System.out.println(test1 == test2);
    System.out.println(test1 == test3);

it prints

false
true

test1 is the string literal "hello" it is allocated in the string pool. test2 is a new string object, different from test1 but with the same character content. It is allocated on the heap. test3 is the result of intern, since there was already the string "hello" in the string pool, we just get back a reference to it. Thus test3 refers to the same object as test1.

Henry
  • 42,982
  • 7
  • 68
  • 84
  • Thanks Henry, but i want to understand how many String objects are created? in case of Case 2 : String object "hello" is created for the first time in heap because of new keyword, then another object with the same contents created , because of intern method but this time it is placed in String pool. Then for test4 reference again one more string object "hello" is created in heap, because of new keyword. So above statements in case 2 created 3 string objects(2 in heap and 1 in string pool)let me know if i am wrong in understanding. – vivek shetty May 18 '14 at 13:25
-1

That is because intern() method returns a interned String which will point to String instance in the intern pool rather than in the heap. If the String instance to be interned is already in the pool, reference to it is returned. If not then the String is added to the pool and reference to it is returned. So you should compare Strings returned by the interned method and not the original references.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • 1
    This is technically incorrect (and confusing). The intern method returns the interned String, which will NOT be a new `String`. It *may* however be a different `String` object to the target object. – Stephen C May 18 '14 at 07:50
  • Technically it is a new String if there is no corresponding String in the String pool. If the String is not in the pool then it is added and reference to this is returned. The original String on the heap is GCed as there will be no references to it. – Aniket Thakur May 18 '14 at 07:57
  • Why downvotes even after editing my answer? Care to comment? And as for my explanation if it is not a new String then how do you justify the output of following program `String s1 = new String("test"); String s2 = new String("test").intern(); System.out.println(s1 == s2);` – Aniket Thakur May 18 '14 at 08:22
  • 1
    The part about 'same String literal instance in the intern pool' is pretty confusing. Try rewording it. – user207421 May 18 '14 at 08:23