【发布时间】:2013-08-16 18:41:33
【问题描述】:
我遇到了这行的问题(在下面评论):
System.out.println("Using == ::"+s3==s4)
输出false。
但是,System.out.println(s3==s4) 输出 true。
现在,我无法理解为什么会得到这个结果:
public class string {
public static void main(String[] args){
String s3="Shantanu";
String s4=s3;
String s1=new String("java");
String s2=new String("javaDeveloper");
System.out.println("Using Equals Method::"+s1.equals(s2));
System.out.println("Using Equals Method::"+s3.equals(s4));
System.out.println("Using == ::"+s3==s4);//Problem is here in this line
System.out.println(s1+"Directly printing the s2 value which is autocasted from superclass to string subclass ");
System.out.println("Directly printing the s1 value which is autocasted from superclass to string subclass "+s2);
System.out.println(s3);
}
}
输出使用等于方法::false
使用等于方法::true
使用 == ::false
java直接打印从超类自动转换为字符串子类的s2值
直接打印从超类自动转换为字符串子类 javaDeveloper 的 s1 值
【问题讨论】:
-
@roippi 你是尖头发的老板吗? ?
-
您的输出与您的代码不一致。第三行应该只是
false。 -
请检查您发布的输出是否正确。显然,开头的字符“Output-”不会出现。加上最后一个
println调用没有显示打印s3并且中间线是可疑的。 -
@roippi 如果您雇用某人是因为他们知道字符串连接比相等比较具有更高的优先级,那么您应得的。为什么不直接问那些 Facebook 问题,“74% 的人做不到这一点。什么是 7 + 3 x 2?”
-
@LeeMeador 你是对的。我也会偷那个问题。谢谢。