As you know, the String().intern() method adds a String value to the string pool if it does not already exist. If exists, it returns a reference to this value / object.
String str = "Cat"; // creates new object in string pool with same character sequence. String st1 = "Cat"; // has same reference of object in pool, just created in case of 'str' str == str1 //that returns true String test = new String("dog"); test.intern();// what this line of code do behind the scene
I need to know when I call test.intern() , what will this intern method do?
add a "dog" with a different link in the row pool or add a link to the test object in the row pool (I think this is not so)?
I tried this
String test1 = "dog"; test == test1 // returns false
I just want to make sure when I call test.intern() , it creates a new object with the same value in the String pool? I now have 2 objects with the value "dog". Does one exist directly on the heap and the other on String?
source share