Integer vs floating point division

Java uses the same operator, / for both integer and floating point (decimal, double) division. When at least 1 of the operands is of type double, the operator performs floating point division. When both operands are of type int, the operator performs integer division.

The result of integer divisions is an int. Any decimal portion of the quotient is discarded.

Examples

System.out.println(25 / 2);      // prints 12

System.out.println(25.0 / 2);    // prints 12.5
System.out.println(25 / 2.0);    // prints 12.5
System.out.println(25.0 / 2.0);  // prints 12.5


int a = 25;
int b = 2;
double c = 2; // stored as 2.0

System.out.println(a / b);             // prints 12
System.out.println((double) (a / b));  // prints 12.0

// Each line below prints 12.5
System.out.println(a / c);
System.out.println((double) a / b);
System.out.println(a / (double) b);
System.out.println((double) a / (double) b);

The most commonly misunderstood example is (double) (a / b). This expression evaluates to 12.0. The division happens before the cast. Both operands are of type int so integer division is peformed. The result of integer division is cast to a double.

Division by zero

System.out.println(5.0 / 0.0);  // prints Infinity
System.out.println(-5.0 / 0.0); // prints -Infinity

System.out.println(0.0 / 0.0);  // prints NaN

System.out.println(5 / 0);      // throws ArithmeticException
System.out.println(0 / 0);      // throws ArithmeticException

In Java, division by zero behaves differently depending on the types of the operands and, if at least one operand is of type double on the numerator.

Modular division

Modular division in Java uses the % symbol. When both operands are of type int and are non-negative (>= 0), modular division returns the remainder from integer division.

(Modular division with floating point values and negative values is also possible. Many other resources discuss the behavior.)

Examples

System.out.println(22 % 5);     // prints 2
System.out.println(20 % 5);     // prints 0
System.out.println(20 % 20);    // prints 0
System.out.println(45 % 1234);  // prints 45

System.out.println(0 % 20);     // prints 0
System.out.println(20 % 0);     // throws ArithmeticException

When a number is modularly divided by a larger number, the result is the original (smaller) number.

As with regular division, 0 can be the left operand for modular division but it cannot be the right operand. Modularly dividing by 0 results in an ArithmeticException.

Determining divisiblity

Modular division has many uses, including determining if a number is divisible by another number. The expression a % b == 0 evaluates to true if and only if a is divisible by b.

Obtaining the last digit of an int

System.out.println(4567 % 10);  // prints 7
System.out.println(456 % 10);   // prints 6
System.out.println(4 % 10);     // prints 4

Modularly dividing an int by 10 evaluates to the last (least significant) digit.

System.out.println(4567 / 10);  // prints 456
System.out.println(456 / 10);   // prints 45
System.out.println(4 / 10);     // prints 0

Dividing an int by 10 evaluates to everything except the last digit.

Wrapping a value

int val = (int) (Math.random() * 26);  // 0 <= val <= 25
int k = (int) (Math.random() * 100);   // 0 <= k <= 99
int newVal = (val + k) % 26;           // 0 <= newVal <= 25

In the example above, newVal will never exceed 25. This technique is commonly used to wrap a value when it may exceed a maximum.

val k val + k (val + k) % 26
23 1 24 24
23 2 25 25
23 3 26 0
23 4 27 1

Comments

Comment on Division operations