Java Reference
In-Depth Information
b.
true
. Note that expressions a and b mean exactly the same thing. Because the
operators
==
and < have higher precedence than
&&,
you do not need to
include the parentheses. The parentheses do, however, make it easier to read.
Most people find the expression in a easier to read than the expression in b,
even though they mean the same thing.
c.
true
.
d.
true
.
e.
false
. Because the value of the first subexpression,
(count == 1)
, is
false
,
you know that the entire expression is
false
without bothering to evaluate
the second subexpression. Thus, it does not matter what the values of
x
and
y
are. This is called short-circuit evaluation, which is what Java does.
f.
true
. Since the value of the first subexpression,
(count < 10)
, is
true
, you
know that the entire expression is
true
without bothering to evaluate the
second subexpression. Thus, it does not matter what the values of
x
and
y
are.
This is called short-circuit evaluation, which is what Java does.
g.
false
. Notice that the expression in g includes the expression in f as a
subexpression. This subexpression is evaluated using short-circuit evaluation
as we described for f. The entire expression in g is equivalent to
!( (true || (x < y)) && true )
which in turn is equivalent to
!( true && true )
, and that is equivalent to
!(true)
, which is equivalent to the final value of
false
.
h. This expression produces an error when it is evaluated because the first
subexpression,
((limit/count) > 7)
, involves a division by zero.
i.
true
. Since the value of the first subexpression,
(limit < 20)
, is
true
, you
know that the entire expression is
true
without bothering to evaluate the
second subexpression. Thus, the second subexpression,
((limit/count) > 7)
,
is never evaluated, so the fact that it involves a division by zero is never
noticed by the computer. This is short-circuit evaluation, which is what
Java does.
j. This expression produces an error when it is evaluated because the first
subexpression,
((limit/count) > 7)
, involves a division by zero.
k.
false
. Since the value of the first subexpression,
(limit < 0)
, is
false
, you
know that the entire expression is
false
without bothering to evaluate the
second subexpression. Thus, the second subexpression,
((limit/count) > 7)
,
is never evaluated, so the fact that it involves a division by zero is never noticed
by the computer. This is short-circuit evaluation, which is what Java does.