Java 双结构相等运算符:if(a==b==c)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20078170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Double structural equality operators: if(a==b==c)
提问by Lan
I wrote some code by accident today and was surprised when Eclipse did not yell at me, for once. The code had a double use of the structural equality operator (==
) similar to the below with the if(a==b==c)
structure.
我今天无意中写了一些代码,当 Eclipse 一次没有对我大喊大叫时,我感到很惊讶。该代码双重使用了结构相等运算符 ( ==
),类似于下面的if(a==b==c)
结构。
public class tripleEqual {
public static void main(String[] args) {
boolean[] a = { true, false };
boolean[] b = { true, false };
boolean[] c = { true, false };
for (int aDex = 0; aDex < 2; aDex++) {
for (int bDex = 0; bDex < 2; bDex++) {
for (int cDex = 0; cDex < 2; cDex++) {
if (a[aDex] == b[bDex] == c[cDex]) {
System.out.printf("Got a==b==c with %d %d %d\n", aDex, bDex, cDex);
}
}
}
}
}
}
The output is
输出是
Got a==b==c with 0 0 0
Got a==b==c with 0 1 1
Got a==b==c with 1 0 1
Got a==b==c with 1 1 0
Playing around, I notice I can't do if(a==b==c)
with any type but boolean
. From that the boolean expression is
玩弄,我注意到我不能做if(a==b==c)
任何类型,但boolean
. 从那个布尔表达式是
( A'. B'. C') + ( A'. B . C ) + ( A . B'. C ) + ( A . B . C')
which simplifies to (A=B).'C + (A<>B).C
.
这简化为(A=B).'C + (A<>B).C
.
Thus, ignoring side-effect, if(a==b==c)
is equal to if(a==b && !c) || (a!=b && c))
.
因此,忽略副作用,if(a==b==c)
等于if(a==b && !c) || (a!=b && c))
。
Can anyone explain how the if(a==b==c)
syntax suggests that?
谁能解释一下if(a==b==c)
语法是如何暗示的?
Edit 1:
编辑1:
I found where my confusion was after so many people explained the left-associativity. Usually I write '1' for true and '0' for false but my minimized truth table/output in the above test, I had '0' for true and '1' for false. The negation of the expression ( A'. B'. C') + ( A'. B . C ) + ( A . B'. C ) + ( A . B . C')
is (A=B)=C
!
在这么多人解释左结合后,我发现了我的困惑。通常我写'1'表示真,'0'表示假,但在上面的测试中我最小化的真值表/输出,我有'0'表示真,'1'表示假。表达式的否定( A'. B'. C') + ( A'. B . C ) + ( A . B'. C ) + ( A . B . C')
是(A=B)=C
!
采纳答案by aga
Playing around, I notice I can't do if(a==b==c) with any type but boolean.
在玩弄时,我注意到我不能使用除布尔值以外的任何类型的 if(a==b==c) 。
You can't do it with any type but boolean
because this comparison chain will be evaluated from the left side to the right. First comparison will be simplified to true
or false
value which has to be compared with the third value in chain (and result of this check will be compared to fourth value and so on, till the end of the chain). As for the primitive values, you can only compare primitives of the same type (e.g. boolean and boolean will work, while double and boolean won't) - that's why you can do it with booleans only - because the ==
returns the value of the same type as all the values in chain.
There's danger here: the result of all that comparison chain isn't equal to true
when all values you've provided are true
. You can see it from the second output: true
== false
== false
raises true
, which is right result if you evaluate it from left to right (as it happens during the program execution), but may seem wrongif you thinkthat this comparison chain has to be evaluated all at once. The correct way to do it is two perform two explicit comparisons:
您不能使用任何类型执行此操作,但因boolean
为此比较链将从左侧到右侧进行评估。第一个比较将简化为必须与链中的第三个值进行比较的值true
或false
值(并且此检查的结果将与第四个值进行比较,依此类推,直到链结束)。至于原始值,您只能比较相同类型的原始值(例如 boolean 和 boolean 可以工作,而 double 和 boolean 不行)-这就是为什么您只能使用 booleans 来执行此操作的原因-因为==
返回的值相同键入为链中的所有值。
这里有危险:true
当您提供的所有值都是true
. 您可以从第二个输出中看到它:true
== false
== false
raises true
,如果你从左到右评估它是正确的结果(就像在程序执行期间发生的那样),但如果你认为这个比较链必须一次评估,这可能看起来是错误的。正确的做法是两个执行两个显式比较:
if (a == b && b == c) {
// do something
}
回答by Dan
The ==
operator is left-associative, so a == b == c
is interpreted as (a == b) == c
. So a == b
returns a bool, which is then compared to c
.
该==
运营商是向左结合,所以a == b == c
被解释为(a == b) == c
。因此a == b
返回一个布尔值,然后将其与c
.
This is a side effect of the parser that is rarely useful in practice. As you've observed, it looks like it does one thing but does something very different (so even if it does what you want, it's not recommended). Some languages actually make the ==
operator non-associative, so a == b == c
is a syntax error.
这是解析器的一个副作用,在实践中很少有用。正如您所观察到的,看起来它只做一件事,但做的事情却大不相同(因此,即使它做了您想要的,也不推荐这样做)。某些语言实际上使==
运算符非关联,因此a == b == c
是语法错误。