简体   繁体   English

为什么选择System.out.println(“嘿s1 == s2:”+ s1 == s2); 打印“false”作为输出而不是打印“hey s1 == s2:false”

[英]Why System.out.println(“hey s1==s2:”+s1==s2); prints “false” as the output instead of printing “hey s1==s2:false”

I have written the following java code: 我写了以下java代码:

String s1 = new String("shan");
String s2 = new String("shan");
String s3="shan";
String s4="shan";
System.out.println("hey s1==s2:"+s1==s2);
System.out.println("s3==s4:"+s3==s4);
System.out.println("s1.equals(s2): "+s1.equals(s2));
System.out.println("s3.equals(s4): "+s3.equals(s4));
System.out.println("s1==s3: "+s1==s3);
System.out.println("s1.equals(s3): "+s1.equals(s3));
System.out.println("hey s1==s2:"+true);

The output: 输出:

false
false
s1.equals(s2): true
s3.equals(s4): true
false
s1.equals(s3): true
hey s1==s2:true

Why does line #5 result in just "false" as the output instead of "hey s1==s2:false"? 为什么第5行导致输出只是“假”而不是“hey s1 == s2:false”?

System.out.println("hey s1==s2:"+s1==s2)

evaluates ("hey s1==s2:"+s1)==s2 , which is false 评估("hey s1==s2:"+s1)==s2 ,这是false

That's why false is printed. 这就是伪造印刷的原因。

The reason for this behavior is that the + operator has a higher precedence than the "==" operator. 这种行为的原因是+运算符的优先级高于“==”运算符。

The following would print what you expected : 以下将打印您的预期:

System.out.println("hey s1==s2:"+(s1==s2))
Line5: System.out.println("hey s1==s2:"+s1==s2);

Because of the operator precedence "hey s1==s2:"+s1 resolving first and then comparing to s2 which leads to false. 由于运算符优先级"hey s1==s2:"+s1首先解析然后与s2比较导致false。

Give the highest precedence to resolve it to correct. 给予最高优先权以解决纠正问题。 Parenthesis have the highest precedence. 括号具有最高优先级。

System.out.println("hey s1==s2:"+(s1==s2));

To comapre strings you have to call s1.equals(s2) . 要comapre字符串,你必须调用s1.equals(s2) the result is true . 结果是true == compares the pointer to the strings and that sare not the same. ==将指针与字符串进行比较,并且不一样。

use brackets for surround separate operations in statement. 在括号中使用括号进行环绕声分离操作。

use 采用

System.out.println("hey s1==s2:"+(s1==s2));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 S1包含带有正则表达式的s2 - S1 contains s2 with regex 改进我的Java方法containsSubstring(s1,s2),它查找s2是否为s1的子字符串 - Improving my Java method containsSubstring(s1, s2) which finds if s2 is a substring of s1 String s1 == String s2(true)但FieldOffset不同 - String s1 == String s2 (true) but FieldOffset is different 返回List的方法 <T> 带参数列表 <S1> ,列表 <S2> ,其中S1和S2延伸T. - Method returning a List<T> with parameters List<S1>, List<S2>, where S1 and S2 extends T 如何更改Student类,以便当s1 = new Student()和s2 = new Student()时,s1 == s2返回true? - How to change Student class so that s1 == s2 return true when s1 = new Student() and s2 = new Student()? 为什么哈希码对于String s1 =“cat”和String s2 = new String(“cat”)是相同的? - Why hash code is same for String s1= “cat” and String s2= new String(“cat”)? 在连接两个String s1和s2时,生成的输出String s3将在java中创建堆或常量池吗? - on concatenating two String s1 and s2, produced output String s3 will create in heap or constant pool in java? 我究竟做错了什么。 将 s1 的值复制到元素 von s2 - What am i doing wrong. copy the the values of s1 into the element von s2 应该使用java将S1的每个备用索引中的字符替换为S2 - Character in each alternate index of S1 should be replaced with S2 using java 使用可比较或比较器接口使用String s1的顺序对String s2进行排序 - Sort String s2 using the order of String s1 using either of comparable or comparator interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM