UNIT 1: USING OBJECTS AND METHODS · TOPIC 1.5
1.5 Casting and Range of Variables
Casting converts between types. The exam tests the truncation of (int), automatic widening to double, and the standard rounding idiom.
What you need to know
- Casting temporarily converts a value to another type:
(int) 3.99is3(truncates toward zero, never rounds);(double) 7is7.0. - A cast applies to the value immediately after it.
(int) 7.8 / 2is3(cast to 7 first, then integer division).(int) (7.8 / 2)is3too but for a different reason (3.9 truncated).(double) 7 / 2is3.5. - Widening is automatic: assigning an int to a double works (
double d = 5;gives 5.0). Narrowing (double to int) requires an explicit cast or the compiler complains. - In mixed arithmetic, ints are promoted to doubles:
5 / 2.0is2.5. - Rounding idiom:
(int) (x + 0.5)rounds a positive double to the nearest int. For negatives,(int) (x - 0.5). - Values outside int's range overflow silently; casting a large double to int clamps to
Integer.MAX_VALUEorMIN_VALUE. - Doubles can't represent most decimals exactly:
0.1 + 0.2is not exactly0.3. Comparing doubles with==is unreliable; check whether the difference is below a small tolerance.
Worked example
double x = 7.6; int a = (int) x; // 7 truncation int b = (int) (x + 0.5); // 8 rounding double c = (double) 7 / 2; // 3.5 double d = (double) (7 / 2); // 3.0 division happened first int e = (int) -7.6; // -7 toward zero, not -8
Lines 4 and 5 are the exam's favorite pair. The cast binds to the nearest value; parentheses change what gets cast.
Exam tip: Ask two things for every cast: what exactly is being cast (the next value only, unless parentheses say otherwise), and is it truncation or rounding (a bare (int) always truncates). Toward zero: (int) -2.9 is -2.
Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- A cast is an operator that applies to the very next value, before any binary operators.
(int) x / ycasts x, then divides.(int) (x / y)divides, then casts. These give different results whenever x / y has a fractional part — and the exam builds questions on exactly that difference. (int)truncates toward zero: 3.99 → 3, −3.99 → −3, 0.5 → 0. It never rounds. If you need rounding, use the +0.5 idiom (positives) or −0.5 (negatives).(double)on an int adds.0: 7 → 7.0. Its main use is forcing real division:(double) a / b.- Automatic widening happens in assignment (
double d = 5;), in arithmetic (int + double → double), and in method calls (passing an int to a double parameter). Narrowing never happens automatically — you must cast. - Casting a double outside int's range clamps to
Integer.MAX_VALUEorMIN_VALUErather than wrapping. Rarely tested, but different from arithmetic overflow. - Precision loss: because doubles can't represent most decimals exactly, a computation like
0.1 * 3gives0.30000000000000004. Comparing doubles for equality is fragile; the exam avoids it or gives you a tolerance approach. - Rounding to two decimal places:
(int) (x * 100 + 0.5) / 100.0. Multiply, round, divide by a double so the result stays decimal.
Mistakes that cost points
- Misplacing the cast's scope.
(int) 7.8 / 2is7 / 2 = 3. Students compute(int) 3.9 = 3— same number by coincidence, wrong reasoning, and it fails on other values. - Rounding instead of truncating.
(int) 2.9is 2, not 3. - Flooring negatives.
(int) -2.9is −2, not −3. Toward zero. - Casting the wrong thing to force real division.
(double) (7 / 2)is 3.0 — division already truncated. Cast an operand, not the result.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 What is the value of
(int) 9.99 / 2 * 2.0?Show answer
Answer: C. (int) 9.99 → 9. 9 / 2 → 4 (integer division). 4 * 2.0 → 8.0.
Q2 Which expression correctly rounds the positive double
val to the nearest integer?Show answer
Answer: B. Adding 0.5 then truncating rounds correctly for positives. Option C truncates first, then adds 0.5 producing a double.
Q3 What is printed by
System.out.println((int) -4.7);?Show answer
Answer: B. Casting truncates toward zero, so -4.7 becomes -4.
Key vocabulary
- Cast
- an explicit conversion, e.g. (int) or (double)
- Truncation
- dropping the fractional part when casting to int
- Widening
- automatic conversion from int to double
- Narrowing
- conversion from double to int, requires a cast