ACSA Exam Prep
Unit 1 · Using Objects and Methods · Topic 1.5

Casting and Range of Variables

Skill 2.ASkill 4.A

Big idea: A cast converts a value to another type. Casting to int chops off the decimal part. Numbers also have limits: ints can overflow, and doubles can be slightly off.

Learning objectives
  • Develop code to cast primitive values to different primitive types in arithmetic expressions and determine the value that is produced as a result.
  • Describe conditions when an integer expression evaluates to a value out of range.
  • Describe conditions that limit accuracy of expressions.

Casting between int and double

Write the type in parentheses before a value: (int) or (double). A cast applies only to the value right after it, so where you put it matters.

Going from int to double happens automatically when needed (widening): double d = 5; stores 5.0. Going from double to int needs an explicit cast, because information can be lost.

Code
int sum = 7;
int count = 2;
System.out.println(sum / count);
System.out.println((double) sum / count);
System.out.println((double) (sum / count));
System.out.println((int) 8.99);
System.out.println((int) -8.99);
Output
3
3.5
3.0
8
-8

Rounding to the nearest integer

Casting truncates toward zero; it never rounds. To round, add 0.5 first for non-negative values, or subtract 0.5 for negative values.

Code
double a = 8.6;
double b = -8.6;
System.out.println((int) (a + 0.5));
System.out.println((int) (b - 0.5));
Output
9
-9

Integer overflow

An int uses 4 bytes, so it can only hold values from Integer.MIN_VALUE (−2147483648) to Integer.MAX_VALUE (2147483647). Going past either end is overflow: no error happens, but the value wraps around to the other end.

Code
int big = Integer.MAX_VALUE;
System.out.println(big);
System.out.println(big + 1);
Output
2147483647
-2147483648

Round-off error in doubles

A double stores a close approximation of most decimal values, so arithmetic can be a tiny bit off. That's why you should compare doubles by checking whether they are close (Math.abs(a - b) < 0.000001) rather than with ==, and why exact counts belong in int.

Code
System.out.println(0.1 + 0.2);
System.out.println(0.1 + 0.2 == 0.3);
System.out.println(Math.abs((0.1 + 0.2) - 0.3) < 0.000001);
Output
0.30000000000000004
false
true
Key points
  • (int) truncates toward zero: (int) 3.99 is 3, (int) -3.99 is -3.
  • (double) a / b casts a first, so the division is real. (double) (a / b) casts after the int division already lost the fraction.
  • Round with (int) (x + 0.5) for x ≥ 0 and (int) (x - 0.5) for x < 0.
  • int values outside Integer.MIN_VALUE..Integer.MAX_VALUE overflow and wrap around.
  • double values can have round-off error.
Watch out
  • int x = 3.0; does not compile. A double never goes into an int without a cast.
  • (int) Math.random() * 10 casts only Math.random(), which is always 0.
Exam tip
  • When you see a cast, put brackets in your head around exactly what it applies to before evaluating.
Quick check
What is stored?
double avg = (double) (9 / 2);
Show answer
4.0. 9 / 2 is 4 using int division, then it becomes 4.0.
What is (int) (2.5 + 0.5)?
Show answer
3.
Not on the exam
  • Special decimal types such as BigDecimal.