Java Reference
In-Depth Information
Try it out by adding it to the program if you like. You should get this output:
Total fruit is 15 and oranges = 5
Integer Division and Remainders
When you divide one integer by another and the result is not exact, any remainder is discarded, so the final
result is always an integer. The division 3 / 2, for example, produces the result 1, and 11 / 3 produces the
result 3. This makes it easy to divide a given quantity equally among a given number of recipients. To divide
numFruit
equally between four children, you could write:
int numFruitEach = 0; // Number of fruit for each child
numFruitEach = numFruit/4;
The result of division when the operands are positive is fairly obvious. It's the result of dividing the right
operand, called the
divisor
, into the left operand, referred to as the
dividend
, a whole number of times. The
situation when either or both operands are negative deserves a little more exploration.
If you divide 7 by −3, the result is −2. Similarly, if you divide −10 by 4 the result is −2. If you divide −5
by −3 the result is +1. The magnitude of the result of dividing a value
a
by a value
b
is the same, regardless
of the sign of the operands, but the sign of the result depends on the sign of the operands. The sign of the
result is positive when the operands both have the same sign and negative when the operands are of different
signs and the divisor is not greater than the dividend (in which case the result is zero). There is one peculiar
exception to this. When the divisor is a negative integer of the largest possible magnitude and the divisor is
−1, the result is the same as the dividend, which is negative and therefore violates the rule. You can see why
this is so by considering specifics.
The value −2_147_483_648 is the negative value of type
int
that has the largest magnitude. Dividing
this by −1 should result in the value +2_147_483_648, but the largest positive integer you can have as type
int
is 2_147_483_647, so this result cannot be represented as type
int
. Therefore, the result is arbitrarily
the original dividend, −2_147_483_648.
Dividing by zero is something you should avoid. If you accidentally cause this to be attempted, your
program terminates because an exception of type
ArithmeticException
is thrown. You will learn what ex-
ceptions are and what you can do about them in Chapter 7.
Of course, there are circumstances where you may want to obtain the remainder after a division, and on
these occasions you can calculate the remainder using the modulus operator,
%
. If you wanted to know how
many fruit were left after dividing the total by 4, you could write the following:
int remainder = 0;
remainder = numFruit%4; // Calculate the remainder after division by 4
When either or both operands to the remainder operator are negative, the result may not seem to be obvi-
ous. Keep in mind, though, that it is related to the divide operation, so if you can work out what the result of
a division is, you can deduce the result of the remainder operation. You can get a clear idea of what happens
by considering a few examples.
The result of the operation 8 % (−3) is +2. This is evident if you recall that from the earlier discussion of
division you know that the result of 8 / (−3) is −2. If you multiply the result of the division by the divisor,
(−2) * (−3), the result is +6, so a remainder of +2 makes sense. The expression (−8) % 3 produces −2, which