AP® Computer Science A · Unit 1 · Topic 1.3

Integer Division and the Mod Operator

When both operands are integers, Java's / operator throws away any remainder instead of rounding, and % gives you back exactly that discarded remainder.

A calculator treats 7 / 2 as a single real number, 3.5. Java treats it differently when both numbers are int values: it asks how many whole times 2 fits into 7, and stops there. 2 fits into 7 three whole times, with 1 left over, so 7 / 2 is 3. The .5 isn't rounded anywhere — it's simply dropped. This is called integer division, and it only behaves this way when both operands are integers; if either one is a double, Java computes the real decimal answer instead.

The mod operator, %, is the partner that rescues what division threw away. 17 / 5 asks how many groups of 5 fit inside 17: three groups, using up 15, with 2 left over. So 17 / 5 is 3, and 17 % 5 is 2, the exact remainder. Picture a number line marked off in jumps of 5: 0, 5, 10, 15, 20. The number 17 lands between the third and fourth jump, two steps past 15. Division counts the jumps you fully completed; mod measures the leftover distance.

This pairing shows up constantly once you start manipulating numbers digit by digit. Dividing by 10 with integer division chops off the last digit of a number, while taking mod 10 reads off exactly that last digit. Together they let you peel a number apart one digit at a time, which becomes a core move in later loops and algorithms built on this unit's idea that every value and every operation has a fixed, predictable type-driven behavior.

Key terms

integer division
Division between two integer operands in Java that discards any remainder and keeps only the whole-number result.
mod operator (%)
An operator that returns the remainder left over after integer division.
truncate
To cut off a value's fractional part without rounding it up or down.
operand
A value that an operator, such as / or %, acts upon.

Watch it explained

  1. How it worksHow does integer division throw the remainder away instead of rounding anything?
  2. How it worksHow does the mod operator hand back exactly the part that division discarded?
  3. Worked exampleFind the value of 17 / 5 and of 17 % 5, shown as groups on a number line.
  4. Worked exampleTrace how 1234 % 10 and 1234 / 10 peel the digits off a number one at a time.
  5. Common mistakeWhy isn't 7 / 2 rounded up to 4 the way ordinary rounding would do it?

Check yourself

Your turn1 of 2
In Java, what does (7·5 + 3) / 5 evaluate to, using integer division?

End of topic.