简体   繁体   中英

java short circuit operators

My doubt is about the precedence of short circuit operators.

Classic example of short circuit operator is below.

if(denom != 0 && num/denom > 10 )

Here usage of shorthand operator allows us to prevent division by zero error because num/denom is never executed.

Now My question is Java says '/' operator has higher precedence than '&&' , then how come left side of '&&' is evaluated before '/'.?

/ has higher precedence than > , which has a higher precedence than && . So, the expression

a && b / c > 0

is the same as

a && (b / c) > 0

which is the same as

a && ((b / c) > 0)

The expression is then evaluated from left to right.

If a is false, then the entire expression is false, without even evaluating the second term.

If a is true, then b / c is first evaluated, and the result is compared to 0.

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