As we know, String().intern()
method add String value in string pool if it's not already exist. If exists, it returns the reference of that 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's 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 this intern method will do?
add "dog" with different reference in string pool or add test
object reference in string pool(i think, it's not the case )?
I tried this
String test1 = "dog";
test == test1 // returns false
I just want to make sure, when I call test.intern()
it creates new object with same value in String pool? now I have 2 objects with value "dog". one exist directly in heap and other is in String pool?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…