Java Reference
In-Depth Information
}
I bolded the lines that have been altered or added. In addition to the change to the numFruit calculation,
I added an extra statement to output the final value of numOranges . The value of numOranges is increased to
6 before the value of numApples is added, so the value of numFruit is 16. Thus, the statement changes the
value stored in numOranges as well as the value stored in numFruit . You could try the decrement operation
in the example as well.
A further property of the increment and decrement operators is that they work differently in an expression
depending on whether you put the operator in front of the variable to which it applies or following it. When
you put the operator in front of a variable, as in the example you have just seen, it's called the prefix form .
The converse case, with the operator following the variable, is called the postfix form . If you change the
statement in the example to
numFruit = numOranges++ + numApples;
and run it again, you find that numOranges still ends up with the value 6, but the total stored in numFruit
has remained 15. This is because the effect of the postfix increment operator is to change the value of
numOranges to 6 after the original value, 5, has been used in the expression to supply the value of numFruit .
The postfix decrement operator works similarly, and both operators can be applied to any type of integer
variable.
As you see, no parentheses are necessary in the expression numOranges++ + numApples . You could even
write it as numOranges+++numApples and it still means the same thing, but it is certainly a lot less obvious
that this is the case. Someone who doesn't have all the rules for evaluating Java expressions at his fingertips
might guess, wrongly, that the expression executes as numOranges+(++numApples) . Such potential confu-
sion is really the programmer's fault. You can write it as
(numOranges++) + numApples
to make it absolutely clear where the ++ operator belongs. It is a good idea to always add parentheses to
clarify things when there is some possibility of misinterpretation.
Computation with Shorter Integer Types
I have deliberately used variables of type int in all the previous examples. Computations with variables
of the shorter integer types introduce some complications. This is because all binary integer operations in
Java work only with both operands of type int or both operands of type long . With arithmetic expressions
using variables of type byte or short , the values of the variables are first converted to type int , and the
calculation is carried out using 32-bit arithmetic. The result is therefore type int — a 32-bit integer. This
has an interesting effect that you can see in the context of the previous example. Try changing the types of
the variables numOranges , numApples , and numFruit in the original version of the program to type short .
For example:
short numOranges = 5;
short numApples = 10;
short numFruit = 0;
You will find that the program no longer compiles. The problem is with this statement:
Search WWH ::




Custom Search