# Incremental Java Integer Division

## Integer Division

/ is the division operator. If the types of the operands are double, then "real" division is performed. Real division is normal division as you learned in grade school. Division is approximate on a computer because you can't do infinitely precise division (recall that double values have finite precision, so there is usually a tiny, but unavoidable error representing real numbers). The result of dividing a double by a double is a double.

However, if both expressions have type int, then the result is an int. Does that sound strange? What is the result of evaluating 7 / 3?

Java does integer division, which basically is the same as regular real division, but you throw away the remainder (or fraction). Thus, 7 / 3 is 2 with a remainder of 1. Throw away the remainder, and the result is 2.

Integer division can come in very handy. Even if you don't see a use for it now, it is used quite often.

What happens if you really want an answer that's double. That is, you want the answer to be 2.33333333 or something close.

One way is to cast the denominator to double. You do this as 7 / (double) 3. Casting is an operator that creates a temporary double value. Thus it creates a temporary 3.0 double.

When one of the operands to a division is a double and the other is an int, Java implicitly (i.e. behind your back) casts the int operand to a double. Thus, Java performs the real division 7.0 / 3.0.