[英]Why System.out.println() doesn't print the string
public class Main {
public static void main(String[] args) {
String str1 = new String("Haseeb");
String str2 = new String("Haseeb");
System.out.println("str1==str2" + str1==str2 );
}
}
The ==
operator is of lower precedence than +
, so +
gets execute first. ==
运算符的优先级低于+
,因此+
首先执行。
"str1==str2" + str1
yields "str1==str2Haseeb"
. "str1==str2" + str1
产生"str1==str2Haseeb"
。
Then ==
executes, and "str1==str2Haseeb"
is not the same object as "Haseeb"
( str2
), so false
is printed. 然后
==
执行,并且"str1==str2Haseeb"
与"Haseeb"
( str2
)不是同一对象,因此打印false
。
You can add parentheses to clarify the desired order of operations. 您可以添加括号以阐明所需的操作顺序。
System.out.println("str1==str2 " + (str1==str2) );
This should print str1==str2 false
. 这应该显示
str1==str2 false
。
(a + b == c)
evaluates as (a + b) == c
, not a + (b==c)
, because +
has higher precedence than ==
. (a + b == c)
求值为(a + b) == c
,而不是a + (b==c)
,因为+
优先级高于==
。 Otherwise arithmetic wouldn't work. 否则算术将不起作用。
What you have there is equivalent to: 您所拥有的等同于:
System.out.println( ("str1==str2" + str1) ==str2 );
And ("str1==str2" + str1)
is not equal to str2
, so you print false
. 并且
("str1==str2" + str1)
不等于str2
,因此您输出false
。
What you probably mean is: 您可能的意思是:
System.out.println("str1==str2 " + (str1==str2));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.