简体   繁体   English

java 短路

[英]short circuit in java

I thought I understood that using short circuit operators that the order of precedence is important however I am having difficulty in understanding why the following code occurs:我以为我理解使用短路运算符优先顺序很重要但是我很难理解为什么会出现以下代码:

line 3. false && true || true   // this returns true
line 4. false && true |  true  //  this returns false

I am correct in stating that the code on line 4 will return false because the evaluation is from right to left.我说第 4 行的代码将返回 false 是正确的,因为评估是从右到左的。 However if line 3 has the left to right evaluation, why does it return a true?但是,如果第 3 行具有从左到右的评估,为什么它返回一个 true? Using just two short circuit operators is fine but using three, I am somewhat stuck on the logic.只使用两个短路运算符很好,但使用三个,我有点卡在逻辑上。 No pun intended.没有双关语的意思。

Remember operator precedence in Java.记住 Java 中的运算符优先级 | is evaluated before && , but ||&&之前进行评估,但是|| is evaluated after && .&&之后进行评估。 Therefore, the first one would evaluate as (false && true) || true因此,第一个将评估为(false && true) || true (false && true) || true which would equal true , while the second one would evaluate as false && (true | true) , which evaluates to false. (false && true) || true将等于true ,而第二个将评估为false && (true | true) ,其评估为 false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM