Skip to content
Marlbridge

AP® Computer Science A · Unit 1 · Topic 1.5

Casting and Range of Variables: Revision Notes (Computer Science A 1.5)

A one-page recap of the (int) and (double) casts, rounding with a cast, the range of int and integer overflow, and round-off error in double.

Short on time? This page is the recap. For explanations and worked examples, use the full study guide.

Recap

  • The casting operators (int) and (double) convert between double and int values.
  • Casting a double to an int truncates: the digits to the right of the decimal point are dropped.
  • Some code widens int values to double automatically (assignment to a double, or an int meeting a double in an operator).
  • A cast has higher precedence than *, / and + and applies to what is immediately to its right.
  • (int) (x + 0.5) rounds a non-negative double; (int) (x - 0.5) rounds a negative one.
  • Integer.MAX_VALUE and Integer.MIN_VALUE are the largest and smallest int. An int uses 4 bytes, so it must be in that range, inclusive.
  • Integer overflow: the result is an int in range but not necessarily what you expected. No exception is thrown.
  • Round-off error: a double more precise than can be stored is rounded to a representable value. Use int values to avoid it.

Key relationships

Expression Value Reason
(int) 3.99 3 truncation
(int) -3.99 -3 truncation toward zero
(double) 7 7.0 widening by cast
(double) 7 / 2 3.5 cast on 7 first, then a double division
(double) (7 / 2) 3.0 the integer division is done first
(int) 2.5 * 2 4 cast applies to 2.5 only
(int) (2.5 * 2) 5 bracket first
(int) (87.5 + 0.5) 88 rounds a non-negative value
(int) (-3.6 - 0.5) -4 rounds a negative value
Integer.MAX_VALUE + 1 Integer.MIN_VALUE overflow wraps around
0.1 + 0.2 0.30000000000000004 round-off error

Assumptions and conventions

  • The only primitive types used are int, double and boolean.
  • Brackets around the expression decide how far a cast reaches.
  • Overflow throws no exception; the program carries on with a wrong value.
  • The exam gives no calculator: do not expect to compute a wrapped overflow value digit by digit, but know that the result is in range and unexpected.

Mistakes to avoid

  1. Treating (int) as rounding. It truncates.
  2. Putting the cast in the wrong place, so a fraction is lost before the cast happens.
  3. Using (int) (x + 0.5) for negative numbers. Use x - 0.5.
  4. Expecting an exception on overflow.
  5. Thinking Integer.MAX_VALUE + 1 is bigger. It wraps to the smallest value.
  6. Believing a double shows every decimal exactly.
  7. Adding a needless cast to store an int in a double.

Quick self-check

  1. What is (int) 9.8? (9)
  2. What is (double) 5 / 4, and what is (double) (5 / 4)? (1.25 and 1.0)
  3. Round 6.5 to the nearest int using a cast. ((int) (6.5 + 0.5) is 7)
  4. What is the value of Integer.MAX_VALUE + 1? (Integer.MIN_VALUE, by overflow)
  5. Why might 0.1 + 0.2 not print exactly 0.3? (Round-off error in the stored double values.)

Next: practice questions.

AP® and Advanced Placement® are trademarks registered by the College Board, which is not affiliated with, and does not endorse, this website. Marlbridge is an independent tuition provider. It is not an authorized AP course provider, an AP school or an exam centre, and these resources are not official College Board materials. Reading them or doing simulations does not meet the laboratory requirement of an AP science course.