【发布时间】:2011-11-28 11:37:57
【问题描述】:
我正在浏览一些字符串示例,我很困惑:
有什么区别
String one = new String("ONE");
和
String one = "ONE";
这就是为什么我有点困惑的例子
String one = new String("ONE");
String another_one = new String("ONE");
System.out.print(one.equals(another_one)); //-->true
System.out.print(one == another_one ); //-->false
同时
String one = "ONE";
String another_one = "ONE";
System.out.print(one.equals(another_one)); //-->true
System.out.print(one == another_one ); //-->true
为什么会这样?
【问题讨论】:
-
Java Strings: "String s = new String("silly");" 和 What is the purpose of the expression “new String(…)” in Java? 和 What is the difference between “text” and new String(“text”) in Java? 的可能重复项(顺便说一下,这些出现在右侧“相关”列表的顶部,与您的列表相同输入问题标题后得到。以后请麻烦查看该列表)
-
确实,这个问题的答案在第二个链接:stackoverflow.com/q/390703
-
差不多这个--> stackoverflow.com/questions/3052442/… question.
-
kjetilod.blogspot.com/2008/09/… 这是可能的原因之一..