Arithmetic and integer division
What you will learn
Arithmetic operators combine numeric values to calculate quantities, costs and measurements. In Java the operand types determine whether division discards the fractional part. Decide whether you need complete groups, a remainder or a fractional measurement before selecting types.
public class Main { public static void main(String[] args) { int notebooks = 29; int capacity = 6; System.out.println(notebooks / capacity); System.out.println(notebooks % capacity); }}Expected output:
45Four complete cartons use 24 notebooks; five remain.
How it works
+ adds, - subtracts, * multiplies, / divides and % gives the remainder. For integer operands, division truncates toward zero: -7 / 3 is -2, not -3. The remainder has the dividend’s sign unless it is zero; the quotient and remainder reconstruct the dividend for a nonzero divisor (within Java integer arithmetic). With a double operand, ordinary integer operands are converted for floating-point arithmetic. Integer division or remainder by zero throws ArithmeticException; floating-point division by zero instead follows IEEE 754 rules, which can produce infinity or NaN. Integer overflow does not automatically throw; even Integer.MIN_VALUE / -1 yields Integer.MIN_VALUE. The Math.addExact method is an optional way to detect addition overflow; exceptions are studied later. Unary - changes the sign, while unary + leaves the promoted numeric value unchanged.
public class Main { public static void main(String[] args) { int distance = 15; int hours = 4; double wrong = distance / hours; double rate = (double) distance / hours; System.out.println(wrong); System.out.println(rate); }}Expected output:
3.03.75Assignment to double happens after the integer division. Casting one operand first (a conversion covered later in this chapter) preserves the fractional result.
Check a boundary or unusual input
public class Main { public static void main(String[] args) { System.out.println(-17 / 5); System.out.println(-17 % 5); System.out.println(Integer.MAX_VALUE + 1); System.out.println(Integer.MIN_VALUE / -1); }}Expected output:
-3-2-2147483648-2147483648The first pair truncates toward zero and retains a negative remainder. The final two computations exceed the positive int range and wrap to the minimum int.
Common mistakes
double mean = 7 / 2; stores 3.0, not 3.5. Write 7 / 2.0 or convert an operand before dividing. Do not use % as a mathematical always-positive modulo when negative inputs are possible.
Try it yourself
A workshop packs 47 bolts into bags of 8. Print the number of full bags and the leftover bolts on separate lines. Repeat with 0 bolts and 16 bolts; capacity stays positive.
Save each example independently as Main.java. Compile with javac Main.java and run with java Main. Local Java practice only; automatic ByteMind Java grading is not established.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Arithmetic and integer division — knowledge check
Apply arithmetic and integer division to examples and boundary conditions.
What is `19 / 4` when both operands are int?
What is `-11 % 4`?
Which expression produces `2.5`?
What happens when an int divisor has value zero?
What is the result of `Integer.MAX_VALUE + 1`?