Integer Division And Remainder

When both operands are integers, / performs integer division. It returns the number of complete groups and discards the fractional part.

The remainder operator % returns what did not fit into those complete groups. Division and remainder together are useful for pages, batches, clocks, and evenly sized batches.

Complete groups and leftovers

public class Main {
    public static void main(String[] args) {
        int files = 17;
        int batchSize = 4;
        int batches = files / batchSize;
        int remaining = files % batchSize;
        String summary = batches + " batches, "
            + remaining + " remaining";
        System.out.println(summary);
    }
}

Output:

4 batches, 1 remaining

Seventeen files form four complete batches of four, with one file remaining. No decimal value is produced.

Division by integer zero throws ArithmeticException. A denominator supplied by a user should be validated before division.

Use division for full boxes and remainder for the cupcakes left over.