01 / 21
\(\frac{7}{2}\)
\(3\)
vs
\(3.5\)

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.

Java

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.

int
32 bits: whole numbers from \(-2^{31}\) to \(2^{31}-1\). No decimals.
double
64 bits: floating-point numbers with ~15 digits of precision. Decimals allowed.
long
64 bits: whole numbers from \(-2^{63}\) to \(2^{63}-1\). No decimals.

The Integer vs. Floating-Point Split

Integer Types byte short int long Truncate decimals
\(7 / 2 = 3\)
(.5 is lost!) vs Floating-Point Types float double Preserve decimals
\(7.0 / 2.0 = 3.5\)
(.5 is kept!)

Why This Matters

Integer division bugs are among the most common errors in Java programs. They cause:

1
Silent data loss: No error is thrown: Java happily returns 3 instead of 3.5 with zero warning.
2
Wrong averages: Computing (88 + 93 + 72) / 3 as integers gives 84 instead of the true average, 84.33....
3
Percentage bugs: 3 / 4 * 100 evaluates to 0 because 3 / 4 truncates to 0 first.

The Core Rule of Java Division

\(\text{int} / \text{int}\)
→ int (truncated)
\(\text{double} / \text{anything}\)
→ double (precise) If EITHER operand is double/float → the other is promoted → result is double

How Truncation Works

0 1 2 3 4
\(3.5\)
(true result)
\(3\)
(int result) truncate
\(7 / 2\)
\(= 3 \text{ (not 3.5)}\)
\(-7 / 2\)
\(= -3 \text{ (not -4)}\)
Truncation moves TOWARD zero, not down.

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

1
Use a double literal: Change 7 / 2 to 7.0 / 2 or 7 / 2.0. Making either operand a double triggers promotion.
2
Cast one operand: Use (double) a / b to promote the variable a before dividing.
3
Declare as double: Store your value in a double variable from the beginning: double x = 7;
4
Multiply first: Rearrange: a * 100 / b can preserve more precision than a / b * 100.

Example 1: Simple Division

int a = 7; int b = 2; int result1 = a / b; // 3 (truncated!) double result2 = a / b; // 3.0 (still truncated!) double result3 = (double) a / b; // 3.5 (correct!)

Example 2: Computing an Average

int score1 = 88; int score2 = 93; int score3 = 72; // Wrong: integer division truncates int avg1 = (score1 + score2 + score3) / 3; // (88 + 93 + 72) / 3 = 253 / 3 = 84 // Correct: cast to double before dividing double avg2 = (score1 + score2 + score3) / 3.0; // 253 / 3.0 = 84.333...

Example 3: The Percentage Trap

int correct = 3; int total = 4; // Wrong: evaluates left to right int pct1 = correct / total * 100; // Step 1: 3 / 4 = 0 (truncated!) // Step 2: 0 * 100 = 0 ← disaster! // Fix A: multiply first int pct2 = correct * 100 / total; // Step 1: 3 * 100 = 300 // Step 2: 300 / 4 = 75 ✓ // Fix B: use double double pct3 = (double) correct / total * 100; // 3.0 / 4 * 100 = 75.0 ✓

The Modulo Operator %

The modulo operator % gives the remainder after integer division. Together, / and % tell the complete story.

\(17 / 5 = 3\)
\(17 \mathbin{\%} 5 = 2\)
Because: \( 5 \times 3 + 2 = 17 \)
\( \text{dividend} = \text{divisor} \times \text{quotient} + \text{remainder} \)

Type Casting in Java

Widening (safe)
int → double: Automatic, no data loss. Java does this implicitly.
Narrowing (risky)
double → int: Requires explicit cast. Truncates the decimal part.
int x = 5; double y = x; // Widening: 5 → 5.0 (automatic) double pi = 3.14159; int z = (int) pi; // Narrowing: 3.14159 → 3 (must cast)

Automatic Promotion Flowchart

\(a / b\)
Either operand a double? YES Promote int → double Result: double NO Both are int Result: int Truncated!

Common Mistakes

✕ Storing in double too late
Writing double x = 7 / 2; still gives 3.0. The division happens as int/int first.
✕ Casting the result
Writing (double)(a / b) casts after truncation. Cast before: (double) a / b.
✕ Dividing before multiplying
3/4*100 = 0 because 3/4 truncates to 0 first. Rearrange to 3*100/4 = 75.

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?