← Back to Week 1 Hub

Integer Division in Java

Workbook 1c, p.57 — Why 10 / 3 equals 3, not 3.333

Try It — Interactive Division Calculator
/
Remainder (%):
Key Examples — Common Gotchas

Click any example to load it in the calculator above.

10 / 3 = 3
Not 3.333! — The .333 is thrown away
7 / 2 = 3
Not 3.5! — No rounding, just truncation
1 / 2 = 0
Not 0.5! — This one really surprises students
99 / 100 = 0
Not 0.99! — Anything smaller ÷ bigger = 0
10 / 5 = 2
Works fine — Divides evenly, no loss
20 / 4 = 5
Works fine — Divides evenly, no loss
Key Rule: Java does NOT round the result. It truncates — meaning it chops off everything after the decimal point. 9 / 5 gives 1, not 2.
How to Fix It — Getting the Full Decimal Result

Solution 1: Use double Variables

double num1 = 10; double num2 = 3; double result = num1 / num2; System.out.println(result); // Output: 3.3333333333333335

Solution 2: Type Casting

int num1 = 10; int num2 = 3; double result = (double) num1 / num2; System.out.println(result); // Output: 3.3333333333333335
Why does casting work? When you write (double) num1 / num2, Java converts num1 to a double first. Then, since one side is a double, Java automatically promotes the other side too. Now it is double / double, which keeps the decimal.
Common Mistake: Writing (double)(num1 / num2) does NOT work! The integer division happens first inside the parentheses (10 / 3 = 3), then you cast 3 to 3.0. The decimal is already lost.
Modulo (%) — The Remainder Integer Division Throws Away
// Integer division gives the quotient int quotient = 10 / 3; // 3 // Modulo gives the remainder int remainder = 10 % 3; // 1 // Together they tell the full story: // 10 = 3 * 3 + 1
10 / 3 = 3 (quotient — how many times 3 fits into 10)
10 % 3 = 1 (remainder — what is left over)
Think of it like long division: 3 goes into 10 three times (9), with 1 left over.
Tip: The modulo operator % is very useful for checking if a number is even or odd: num % 2 == 0 means even, num % 2 != 0 means odd.

Enter any two integers and click Divide to see the truncation in action.

← Java Naming Conventions Pre/Post Increment →