Short-Circuit Evaluation of Complex Conditions
The parts of an expression containing &&
or ||
operators are evaluated only until it’s known whether the condition is true or false. Thus, evaluation of the expression
(gender == FEMALE) && (age >= 65)
stops immediately if gender
is not equal to FEMALE
(i.e., the entire expression is false
) and continues if gender
is equal to FEMALE
(i.e., the entire expression could still be true
if the condition age >= 65
is true
). This feature of conditional AND and conditional OR expressions is called short-circuit evaluation.
Common Programming Error 5.8
In expressions using ...
Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.