Java JDK如何在字符串池中创建新字符串

时间:2017-08-30 06:58:06

标签: java string runtime string-pool

我了解到,当您使用字符串分配值时,它会使用字符串池。所以,他们使用相同的地址

String S1 = “Hello World”;
String S2 = “Hello World”;

String S3 = “Hello”;
String S4 = “ World”;

System.out.println(S1 == S2); // Returns true

但是当我尝试按照以下方式进行时

S3 = S3 + S4;

用S1确认地址......

System.out.println(S1 == S3); // Returns false

为什么会返回false?为什么JDK不使用与S1或S2相同的地址?

谢谢!

0 个答案:

没有答案