本文介绍了打印字符串比较结果时得到奇怪的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到了这一行的问题(如下所示):
I am facing a problem with this line (commented below):
System.out.println("Using == ::"+s3==s4)
输出 false
。
然而, System.out.println(s3 == s4)
输出 true
。
现在,我无法理解为什么会得到这个结果:
Now, I am not able to understand why I am getting this result:
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);
}
}
Output-Using Equals Method::false
Using Equals Method::true
Using == ::false
java Directly printing the s2 value which is autocasted from superclass to string subclass
Directly printing the s1 value which is autocasted from superclass to string subclass javaDeveloper
推荐答案
你缺少一组括号:
System.out.println("Using == ::" + (s3==s4));
在您的版本中,使用== ::+ s3
正在通过 ==
与 s4
进行比较,这不是你想要的。
In your version, "Using == ::" + s3
is being compared via ==
to s4
, which isn't what you want.
通常, +
有更高的比 ==
,这就是为什么使用== ::+ s3 == s4
正在评估为(使用== ::+ s3)== s4
。
In general, +
has a higher precedence than ==
, which is why "Using == ::" + s3==s4
is being evaluated as ("Using == ::" + s3) == s4
.
这篇关于打印字符串比较结果时得到奇怪的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!