2. Division
% (remainder operator, modulus operator)
- returns the remainder after dividing the
second operand to the first
19 % 4 = 3
4 % 9 = 4
3. Division
% - the sign of the result is the sign of the
numerator (first operand)
-10 % 3 = -1
10 % -3 = 1
4. Division
/ - results depend on the type of the
operand
• if both operands are integers (byte, short,
int, long) the result is integer, any fractional
part of the result is discarded
• if either or both operands are floating point
(float, double) the result is floating point
5. Division
14 / 5 = 2
14.0 / 5 = 2.8
14 / 5.0 = 2.8
14.0 / 5.0 = 2.8
(double) 14 / 5 = 2.8
(double) (14 / 5) = 2.0 // in this case the integer division
is performed first and afterwards the type casting
6. Summary
% (remainder operator, modulus operator)
- returns the remainder after dividing the
second operand to the first
/ - returns the results of division; result
depends on the type of the operand