关于 String#intern

23 天前
 lixiaolin123

public Class Example{ public static void main(String[] args) { String s = new String("1"); s.intern(); String s2 = "1"; System.out.println(s == s2);

String s3 = new String("1") + new String("1");
s3.intern();
String s4 = "11";
System.out.println(s3 == s4);

} }

这段代码的我机器上输出结果是 false false.但我看到原文 https://tech.meituan.com/2014/03/06/in-depth-understanding-string-intern.html 的结果是 false true.所以到底结果应该是什么?

812 次点击
所在节点    问与答
2 条回复
newte88
23 天前
s = s.intern();
lixiaolin123
22 天前
@newte88 感谢回答。

我找到原因了。 一开始得到结果 false false 是因为我用的是公共在线 JVM https://www.bejson.com/runcode/java/

第 2 条附言,比较一下代码 1 和代码 2 ,你应该也就知道原因了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1068388

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX