Java Reference
In-Depth Information
SR 2.23
After executing the statement,
diameter
holds the value 20. First, the
current value of
diameter
(5) is multiplied by 4, and then the result is
stored back in
diameter
.
SR 2.24
Operator precedence is the set of rules that dictates the order in which
operators are evaluated in an expression.
SR 2.25
The evaluations of the expressions are
a.
15 + 7 * 3
15 + 21
36
b.
(15 + 7) * 3
22 * 3
66
c.
3 * 6 + 10 / 5 + 5
18 + 2 + 5
25
d.
27 % 5 + 7 % 3
2 + 1
3
e.
100 / 2 / 2 / 2
50 / 2 / 2
25 / 2 = 12
f.
100 / ( 2 / 2) / 2
100 / 1 / 2
100 / 2 = 50
SR 2.26
Expression a is valid. Expression b is invalid because there are two
open parentheses but only one close parenthesis. Similarly with expres-
sion c, where there are two open parentheses but no close parenthesis.
Expression d might be a valid algebraic expression in an algebra book,
but it is not a valid expression in Java. There is no operator between the
operands 2 and ( 4 ).
SR 2.27
After the sequence of statements, the value in
result
is 8.
SR 2.28
After the sequence of statements, the value in
result
is 8. Note that
even though
result
was set to
base + 3
, changing the value of
base
to
7 does not retroactively change the value of
result
.
SR 2.29
An assignment operator combines an operation with assignment. For
example, the
+=
operator performs an addition, then stores the value
back into the variable on the left-hand side.
SR 2.30
After executing the statement,
weight
holds the value 83. The assign-
ment operator
−=
modifies
weight
by first subtracting 17 from the cur-
rent value (100), then storing the result back into
weight
.
2.5
Data Conversion
SR 2.31
A widening conversion tends to go from a small data value, in terms
of the amount of space used to store it, to a larger one. A narrowing
conversion does the opposite. Information is more likely to be lost in a
narrowing conversion, which is why narrowing conversions are consid-
ered to be less safe than widening ones.
SR 2.32
The conversions are: a. widening, b. narrowing, c. widening, d. widening,
e. widening.
Search WWH ::
Custom Search