Integer Division &
Numeric Types
Master how Java handles division between integers, understand when decimals vanish, and learn to choose the right numeric type every time.
What Are Numeric Types?
Java stores numbers in fixed-size memory containers called primitive types. Each type determines the range of values and whether decimal places are allowed.
The Integer vs. Floating-Point Split
Why This Matters
Integer division bugs are among the most common errors in Java programs. They cause:
The Core Rule of Java Division
How Truncation Works
int vs. double: When to Use Each
| int | double | |
|---|---|---|
| Stores decimals? | ✕ No | ✓ Yes |
| Memory | 32 bits (4 bytes) | 64 bits (8 bytes) |
| Division behavior | Truncates | Preserves |
| Use for | Counting, indices, loops | Averages, ratios, science |
| Default literal | 42 | 42.0 |
| Precision issues? | Exact | ~15 digits |
How to Fix Integer Division
Example 1: Simple Division
Example 2: Computing an Average
Example 3: The Percentage Trap
The Modulo Operator %
The modulo operator % gives the remainder after integer division. Together, / and % tell the complete story.
Type Casting in Java
Automatic Promotion Flowchart
Common Mistakes
Summary: Division Outcomes
| Expression | Result Type | Value | Why |
|---|---|---|---|
| 7 / 2 | int | 3 | Both int, truncated |
| 7.0 / 2 | double | 3.5 | 7.0 is double → promotes 2 |
| 7 / 2.0 | double | 3.5 | 2.0 is double → promotes 7 |
| (double) 7 / 2 | double | 3.5 | Cast first, then divide |
| (double)(7 / 2) | double | 3.0 | Divide first (truncated), then cast |
| 7 % 2 | int | 1 | Remainder of 7 / 2 |
Question 1 of 5
What is the value of result after this code runs?
int result = 9 / 4;
Question 2 of 5
What does this expression evaluate to?
double x = 5 / 2;
Question 3 of 5
Which expression correctly computes 75% (as a percentage) given int correct = 3; and int total = 4;?
Question 4 of 5
What is the value of r?
int r = -11 % 3;
Question 5 of 5
What is the output of this code?
int a = 10;
int b = 3;
double c = a / b;
double d = (double) a / b;
System.out.println(c + " " + d);