[英]How is (true != false != true) different from (false != true != false)?
I recently took an entry test in Java and this question confused me. 我最近用Java进行了入学测试,这个问题使我感到困惑。 The full question is:
完整的问题是:
boolean b1 = true;
boolean b2 = false;
if (b2 != b1 != b2)
System.out.println("true");
else
System.out.println("false");
My first question is what (b2 != b1 != b2) means and the second question, as specified in the title, is how (false != true != false) evaluates to true while (true != false != true) evaluates to false (I tested that on Netbeans). 我的第一个问题是(b2!= b1!= b2)的含义,第二个问题,如标题中所述,是(false!= true!= false)如何计算为true,而(true!= false!= true)评估为假(我在Netbeans上进行了测试)。
You have two boolean comparisons where the first comparison produces a result that is compared to another boolean value (the last one). 您有两个布尔比较,其中第一个比较产生的结果与另一个布尔值(最后一个)进行比较。
And the equality operators are syntactically left-associative (they group left-to-right). 并且相等运算符在语法上是左关联的(它们的组从左到右)。
To understand you can rewrite the actual comparison by doing the comparison in two times : 要理解,您可以通过两次比较来重写实际比较:
1) false != true != false
== true
as 1)
false != true != false
== true
为
boolean result = false != true; // ->true
result = true != false; // ->true
result == true;
2) true != false != true
== false
as 2)
true != false != true
== false
为
boolean result = true != false; // -> true
result = true != true; // -> false
result == false;
Or you can also enclose the fist comparison by parenthesis to ease the reading of the evaluations precedence (left to right) : 或者,您也可以用括号将第一比较括起来,以简化对评估优先级的读取(从左到右):
1) false != true != false
== true
as 1)
false != true != false
== true
为
<=> (false != true) != false
<=> true != false
<=> true
2) true != false != true
== false
as 2)
true != false != true
== false
为
<=> (true != false) != true
<=> true != true
<=> false
it will evaluate this way : 它将以这种方式评估:
1- false != true != false
= false!=true
= true
1-
false != true != false
= false!=true
= true
so this false != true
became true
and then equation become like this 所以这个
false != true
变成true
,然后方程式变成这样
true
!= false
which is equal to true
true
!= false
等于true
so the result is true
. 所以结果是
true
。
Now for second one you can evaluate the same way as 现在,对于第二个,您可以评估与
2- true != false != true
2-
true != false != true
true!=false
which is true
true!=false
这是true
now true != true
which is false
so you are getting result as false
现在为
true != true
,这是false
因此您得到的结果为false
See what happens most of the compiler start evaluating the expression from left to right So in this case what is happening first it is evaluating this. 看看大多数编译器会发生什么,从左到右开始评估表达式。因此,在这种情况下,首先发生的事情就是对其进行评估。
False != True == True //is evaluated first which is true
Then it evaluates with this true we got from the first expression 然后用从第一个表达式得到的真值进行评估
True != False == True //which is also true
So the completer expression goes like this 所以完整的表达像这样
False != True != False == True// which is True
Now in the second case the expression is like 现在在第二种情况下,表达式就像
True != False != True == False
the outputs for fist expression which is True != False evaluates for True and expression becomes True != True which is false 拳头表达式的输出为True!= False的结果为True,表达式变为True!= True为false
I hope it makes sense. 我希望这是有道理的。 It's because the associativity of != is from left to right so it evaluating from left to right.
这是因为!=的关联性是从左到右,因此从左到右进行评估。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.