【发布时间】:2016-04-12 05:39:53
【问题描述】:
为什么在使用相等运算符'=='比较时,使用具有相同字符串文字的String类构造函数声明的两个字符串对象不相等,但通过分配相同的字符串文字直接声明时是相等的。
String s1 = new String("hello");
String s2 = new String("hello");
Boolean result1 = (s1 == s2);// returns false
String s3 = "hello";
String s4 = "hello";
Boolean result2 = (s3 == s4);// returns true
【问题讨论】:
-
这个答案是对该问题的最佳解释......