我正面临着这条线的问题(下面评论):
System.out.println("Using == ::"+s3==s4)
Run Code Online (Sandbox Code Playgroud)
哪个输出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);
}
}
Run Code Online (Sandbox Code Playgroud)
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
ars*_*jii 33
你错过了一组括号:
System.out.println("Using == ::" + (s3==s4));
Run Code Online (Sandbox Code Playgroud)
在你的版本,"Using == ::" + s3正在通过比==对s4,这是不是你想要的.
一般情况下,+具有更高的优先级比==,这就是为什么"Using == ::" + s3==s4被评价为("Using == ::" + s3) == s4.
anu*_*ava 13
您正在使用此代码:
System.out.println("Using == ::"+ s3==s4);
Run Code Online (Sandbox Code Playgroud)
正在评估为:
System.out.println( ("Using == ::" + s3) == s4);
Run Code Online (Sandbox Code Playgroud)
因此,你输出的结果是假的.
原因是运算符优先级+高于==此Operator Precedence表:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
正如其他答案所说,您需要使用包含布尔表达式的括号:
System.out.println("Using == ::" + (s3==s4));
Run Code Online (Sandbox Code Playgroud)
这条线是正确的:
"Using == ::"+s3 不等于 s4
您需要更改代码:
"Using == ::"+(s3==s4)
Run Code Online (Sandbox Code Playgroud)
编辑: 给定代码的输出是:
Using Equals Method::false
Using Equals Method::true
false
javaDirectly 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
Shantanu
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
757 次 |
| 最近记录: |