Home Segments Top Top Previous Next

85: Mainline

When an integer denominator does not divide evenly into an integer numerator, the division operator rounds the result toward zero, producing another integer. The modulus operator, %, produces the integer remainder:

5 / 3           // Divide, evaluating to 1 
-5 / 3          // Divide, evaluating to -1 
5 % 3           // Divide, evaluating to the remainder, 2 
-5 % 3          // Divide, evaluating to the remainder, -2 

Of course, when Java divides floating-point numbers, it produces a floating-point result:

5.0 / 3.0       // Divide, evaluating to 1.66667