Java Reference
In-Depth Information
numFruit = numOranges + numApples;
Because the expression
numOranges + numApples
produces a 32-bit result, the compiler cannot store
this value in
numFruit
, as the variable
numFruit
is only 16 bits long. To make the code acceptable to the
compiler, you must modify the assignment statement so that the 32-bit result of the addition is converted
back to a 16-bit number. You do this by changing the statement to:
numFruit = (short)(numOranges + numApples);
The statement now calculates the sum of
numOranges
and
numApples
and then converts, or
casts
, the
32-bit result to type
short
before storing it in
numFruit
. This is called an
explicit cast
, and the conversion
process is referred to as
casting
. The cast to type
short
is the expression
(short)
, and the cast applies
to whatever is immediately to the right of
(short)
, so the parentheses around the expression
numOranges
+ numApples
are necessary. Without them the cast applies only to the variable
numOranges
, which is type
short
anyway, and the code still does not compile.
If the variables here were of type
byte
, you would need to cast the result of the addition to type
byte
.
You would write such a cast as
(byte)
. This is a strong clue to how you write casts to other types. In gener-
al, you write a cast to any given type,
typename
, as the
typename
between parentheses — thus
(typename)
.
The effect of the cast to type
short
in the example is just to take the least significant 16 bits of the result,
discarding the most significant 16 bits. The least significant bits are those at the right-hand end of the num-
ber because the bits in a binary number in Java increase in value from right to left. Thus, the most significant
bits are those at the left-hand end. For the cast to type
byte
only the least significant 8 bits are kept. This
means that if the magnitude of the result of the addition is such that more than 16 bits are necessary to rep-
resent it (or 8 bits in the case of a cast to
byte
), your answer will be wrong. You get no indication from the
compiler that this has occurred because it was you, after all, that expressly specified the cast, and the com-
piler assumes that you know what you are doing. To minimize the possibility for such hidden and mystifying
errors, you should avoid explicit casts in your programs unless they are absolutely essential.
An integer arithmetic operation involving a value of type
long
is always carried out using 64-bit values.
If the other number in such an operation is not of type
long
, the compiler arranges for it to be cast to type
long
before the operation is executed. For example:
long result = 0;
long factor = 10L;
int number = 5;
result = factor*number;
To execute the last statement, the multiplication is carried out using
long
values because the variable
factor
is of type
long
. The value stored in
number
is converted to type
a
, and that is multiplied by the value
of
factor
.
All other integer arithmetic operations involving types other than
long
are carried out with 32-bit values.
Thus, you really need to consider only two kinds of integer literals:
• Type
long
for operations with 64-bit values where the value has an
L
appended
• Type
int
for operations with 32-bit values for all other cases where there is no
L
at the end of the
number
Errors in Integer Arithmetic