2.5 Integer Division and Remainder If both operands of the division operator / are of an integer type, then Java performs integer division. Integer division always results in an integer value?aany fraction in the quotient is simply truncated, or chopped off. We can think of this as rounding down the quotient to the next integer closer to zero, if the quotient isn't already an integer. The remainder operator % gives the remainder of performing an integer division of its two operands. Given any two integer values m and n,
always equals m. From that, we can deduce the sign of the result of the remainder operation, as shown in Table 2-3 . The sign of the first operand determines the sign of the remainder. As we mentioned in Section 2.1, the division and remainder operations will throw the ArithmeticException if we attempt to divide by zero. |
Top |