Special string case interned in Java

I have two lines in Java code:

String str = new String("My place")
String str1 = new String("My place")

      

It is clear that the new String ("My place") creates two objects, one of which is related to interning and the other due to new , but I am confused as here the argument has the same literal, so the same interned an object is used by str1 which results in 3 objects or others which results in 4 objects

+3


source to share


2 answers


Internationalization of string literals is automatic in Java, so the same interned object will be used in both constructors, so there will be three objects instead of four.



+3


source


the same interned object will be used by str1 which will result in 3 objects, try using equal methods



-2


source







All Articles