Java Reference
In-Depth Information
Short-Circuit and Complete Evaluation
Java takes an occasional shortcut when evaluating a Boolean expression. Notice that in
many cases, you need to evaluate only the first of two or more subexpressions in a
Boolean expression. For example, consider the following:
(savings >= 0) && (dependents > 1)
If savings is negative, then (savings >= 0) is false , and, as you can see in the
tables in Display 3.5, when one subexpression in an && expression is false , then
the whole expression is false , no matter whether the other expression is true or
false . Thus, if we know that the first expression is false , there is no need to eval-
uate the second expression. A similar thing happens with || expressions. If the
first of two expressions joined with the || operator is true , then you know the
entire expression is true , whether the second expression is true or false . In some
situations, the Java language can and does use these facts to save itself the trouble
of evaluating the second subexpression in a logical expression connected with an
&& or an || . Java first evaluates the leftmost of the two expressions joined by an &&
or an || . If that gives it enough information to determine the final value of the
expression (independent of the value of the second expression), then Java does not
bother to evaluate the second expression. This method of evaluation is called
short-circuit evaluation or lazy evaluation .
Now let's look at an example using && that illustrates the advantage of short-circuit
evaluation, and let's give the Boolean expression some context by placing it in an if
statement:
short-circuit
evaluation
lazy
evaluation
if ( (kids != 0) && ((pieces/kids) >= 2) )
System.out.println("Each child may have two pieces!");
If the value of kids is not zero, this statement involves no subtleties. However, sup-
pose the value of kids is zero and consider how short-circuit evaluation handles this
case. The expression (kids != 0) evaluates to false , so there would be no need to
evaluate the second expression. Using short-circuit evaluation, Java says that the
entire expression is false , without bothering to evaluate the second expression. This
prevents a run-time error, since evaluating the second expression would involve
dividing by zero.
Java also allows you to ask for complete evaluation . In complete evaluation, when
two expressions are joined by an “and” or an “or,” both subexpressions are always eval-
uated, and then the truth tables are used to obtain the value of the final expression. To
obtain complete evaluation in Java, you use & rather than && for “and” and use | in
place of || for “or.”
In most situations, short-circuit evaluation and complete evaluation give the same
result, but, as you have just seen, there are times when short-circuit evaluation can
avoid a run-time error. There are also some situations in which complete evaluation is
preferred, but we will not use those techniques in this topic. We will always use && and
|| to obtain short-circuit evaluation.
complete
evaluation
Search WWH ::




Custom Search