Java OR 运算符优先级
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21202415/
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
Java OR operator precedence
提问by Szymon Toda
How to chain conditional statements in Java in a way that if b
is false, than do not check c
?
如何以如果b
为假而不是不检查的方式在 Java 中链接条件语句c
?
If a
and c
are false, and b
is true, does c
will be checked?
如果a
和c
是假的,并且b
是真的,是否c
会被检查?
if (a || b || c)
I am looking for similar feature that PHP holds with difference between OR
and ||
我在寻找类似的功能,PHP与持有之间的差异OR
和||
采纳答案by Silly Freak
The Java ||
operator (logical or operator) does not evaluate additional arguments if the left operand is true
. If you wanted to do that, you can use |
(bitwise or operator), although, according to the name, this is a little of a hack.
||
如果左操作数是 ,则Java运算符(逻辑或运算符)不计算附加参数true
。如果你想这样做,你可以使用|
(按位或运算符),尽管根据名称,这有点像黑客。
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.23
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.23
Thus, && computes the same result as & on boolean operands. It differs only in that the right-hand operand expression is evaluated conditionally rather than always.
因此, && 在布尔操作数上计算与 & 相同的结果。它的不同之处仅在于右侧操作数表达式是有条件地而不是始终进行计算的。
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.24
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.24
Thus, || computes the same result as | on boolean or Boolean operands. It differs only in that the right-hand operand expression is evaluated conditionally rather than always.
因此,|| 计算与 | 相同的结果 在布尔或布尔操作数上。它的不同之处仅在于右侧操作数表达式是有条件地而不是始终进行计算的。
Extra: it is considered bad programming style to depend on these semantics to conditionally call code with side effects.
额外:依赖这些语义来有条件地调用具有副作用的代码被认为是糟糕的编程风格。
回答by Louis
回答by Weibo Li
c
won't be checked. To prove this, you can use the following code:
c
不会被查。为了证明这一点,您可以使用以下代码:
boolean a() {
System.out.println("a")
return false;
}
boolean b() {
System.out.println("b")
return true;
}
boolean c() {
System.out.println("c")
return true;
}
void test() {
if(a() || b() || c()) {
System.out.println("done")
}
}
The output will be:
输出将是:
a
b
done
回答by Mitesh Pathak
1) How to chain conditional statements in Java in a way that if bis false, than do not check c?
1) 如何在 Java 中链接条件语句,如果b为假,则不检查c?
- Why do you need that?? it kills OR logic - neways
c
then is immaterial - so your answer simply is
if (a || b)
- or may be you are looking for
if (a || (b && c))
- 你为什么需要那个??它扼杀了 OR 逻辑 -
c
那么neways是无关紧要的 - 所以你的答案就是
if (a || b)
- 或者你正在寻找
if (a || (b && c))
2) If a and c are false, and b is true, does c will be checked? [ if (a || b || c) ]
2)如果a和c为假,b为真,c会被检查吗?[ 如果 (a || b || c) ]
- Nope
||
does short circuit evaluation. So ifa
isfalse
,b
will be checked and ifb
is true,c
won't be checked
- 不
||
做短路评估。所以如果a
是false
,b
将被检查,如果b
是真的,c
不会被检查
For more on short circuit evaluation: refer
有关短路评估的更多信息:请参阅
回答by Rakesh Chaudhari
if (a || b || c)
如果 (a || b || c)
for you case,
对于你的情况,
a is true then b and c not checked.
a 为真,则 b 和 c 未检查。
a is false and b is true then c not checked.
a 为假,b 为真,则不检查 c。
a and b are false then c is checked.
a 和 b 为假,然后检查 c。
the left side of the operator is evaluated first
首先评估运算符的左侧