简体   繁体   中英

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. However if line 3 has the left to right evaluation, why does it return a 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. | is evaluated before && , but || is evaluated after && . Therefore, the first one would evaluate as (false && true) || true (false && true) || true which would equal true , while the second one would evaluate as false && (true | true) , which evaluates to false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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