java short circuit operators -
my uncertainty precedence of short circuit operators.
classic illustration of short circuit operator below.
if(denom != 0 && num/denom > 10 )
here usage of shorthand operator allows prevent partition 0 error because num/denom never executed.
now question java says '/' operator has higher precedence '&&' , how come left side of '&&' evaluated before '/'.?
/
has higher precedence >
, has higher precedence &&
. so, expression
a && b / c > 0
is same as
a && (b / c) > 0
which same
a && ((b / c) > 0)
the look evaluated left right.
if a
false, entire look false, without evaluating sec term.
if a
true, b / c
first evaluated, , result compared 0.
java operators
No comments:
Post a Comment