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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:16:29  来源:igfitidea点击:

Java OR operator precedence

javaoperator-precedence

提问by Szymon Toda

How to chain conditional statements in Java in a way that if bis false, than do not check c?

如何以如果b为假而不是不检查的方式在 Java 中链接条件语句c

If aand care false, and bis true, does cwill be checked?

如果ac是假的,并且b是真的,是否c会被检查?

if (a || b || c)

I am looking for similar feature that PHP holds with difference between ORand ||

我在寻找类似的功能,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

If ais false and bis true, then cwill not be evaluated.

如果a为假且b为真,则c不会被评估。

See this answerthat addresses short circuiting of logical operators in Java.

请参阅解决 Java 中逻辑运算符短路问题的答案

回答by Weibo Li

cwon'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 cthen 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 if ais false, bwill be checked and if bis true, cwon't be checked
  • ||做短路评估。所以如果afalseb将被检查,如果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

首先评估运算符的左侧