If you have this:
String s1 = "Hello".concat("World");
String s2 = s1.intern();
System.out.println(s1 == s2);
... s1.intern()
adds s1
to the pool and returns s1
, because there is no equivalent string in the pool. So naturally s1 == s2
.
But when you have this:
String s1 = "Hello".concat("World");
String s3 = new String("HelloWorld");
String s2 = s1.intern();
System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s2 == s3);
... "HelloWorld"
( ). s1.intern()
, s1
. s1 == s2
.
, :
String s1 = "Hello".concat("World");
String sx = "HelloWorld";
String s3 = new String(sx);
String s2 = s1.intern();
System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s2 == s3);
System.out.println(s1 == sx);
System.out.println(s2 == sx);
sx
- , .
, , s1 "HelloWorld"
, concat
. s1
, s1.intern()
, . "-2" , , "-2" : "HelloWorld"
.