Skip to main content
United StatesComputer ScienceSyllabus dot point

How does casting convert between int and double, and what is the range of an int?

Topic 1.5 Casting and Ranges of Variables: use casting to convert between int and double, predict the effect of truncation, and recognize that an int has a finite range that can overflow.

A focused answer to AP CSA Topic 1.5, covering explicit casting between int and double, the truncation that casting to int causes, where the cast applies in an expression, the finite range of an int, and integer overflow, with a traced worked example.

Generated by Claude Opus 4.89 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. Casting between int and double
  3. Where the cast applies
  4. The range of an int
  5. Try this

What this topic is asking

The College Board (Topic 1.5) wants you to use casting to convert between int and double, to predict the truncation that casting to int causes, to know where in an expression the cast applies, and to recognize that an int has a finite range so very large values can overflow. Casting is the fix for the integer-division trap from Topic 1.3, and it is tested constantly.

Casting between int and double

Java widens an int to a double automatically, but narrowing a double to an int loses information, so it must be done explicitly with a cast. Casting to int truncates toward zero:

  • (int) 3.9 is 3 (not 4 - it does not round).
  • (int) -3.9 is -3 (toward zero).
  • (double) 5 is 5.0.

Where the cast applies

This is the single most tested idea in Topic 1.5. To average two int variables sum and count as a real number, cast before the division:

double average = (double) sum / count;

Casting sum makes the whole division floating-point. Writing (double) (sum / count) would truncate first and only then add .0.

The range of an int

An int is a 32-bit signed integer, so it can store values only between Integer.MIN_VALUE (-2147483648) and Integer.MAX_VALUE (2147483647). When a calculation produces a value outside this range, the int overflows: instead of erroring, it silently wraps around to the far end of the range, often producing a large negative number from a positive calculation. For example, Integer.MAX_VALUE + 1 wraps to Integer.MIN_VALUE.

A double has a far larger range and can represent very large and very small magnitudes, though only approximately (it can lose precision). When a calculation might exceed the int range, store it in a double or be aware overflow can occur. The exam expects you to recognize that overflow is possible and that it does not throw an error - it just gives a wrong, wrapped value.

Try this

Q1. State the value of (int) 7.99. [1 point]

  • Cue. 7. Casting to int truncates toward zero; it never rounds up.

Q2. Explain why (double) (9 / 2) is 4.0 but (double) 9 / 2 is 4.5. [2 points]

  • Cue. In the first, 9 / 2 is integer division (4) before the cast, so casting gives 4.0. In the second, 9 is cast to 9.0 first, so 9.0 / 2 is floating-point division giving 4.5.

Exam-style practice questions

Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AP 2022 (style)1 marksMultiple choice. What is printed by the following code segment? ```java double avg = (double) (17 / 5); System.out.println(avg); ``` (A) `3.4` (B) `3.0` (C) `3` (D) `4.0` (E) `3.5`
Show worked answer →

The answer is (B).

The cast applies to the result of (17 / 5), which is computed first as integer division: 17 / 5 is 3. Then (double) 3 is 3.0. Casting after the division cannot recover the lost fractional part. To get 3.4 you would cast before dividing: (double) 17 / 5. (A) assumes the cast happened before the division; (C) ignores that avg is a double and prints with a decimal; (D) and (E) miscompute.

Markers reward seeing that the parentheses force integer division before the cast, so the answer is 3.0, not 3.4.

AP 2020 (style)4 marksFree response (code writing). Two `int` variables `points` and `games` are given, with `games` greater than 0. Write a code segment that computes the average points per game as a `double` (keeping the fractional part) and stores it in a `double` variable `average`. Then print `average` truncated to a whole number (the integer part only) as an `int`.
Show worked answer →

A 4-point question testing casting in both directions.

double average = (double) points / games;
int wholePart = (int) average;
System.out.println(average);
System.out.println(wholePart);

Point 1: casting points to double before dividing so the division is floating-point and keeps the fraction. Point 2: dividing by games. Point 3: casting average to int truncates toward zero to get the whole part. Point 4: storing and printing each correctly. Writing (double) (points / games) would truncate first and lose the fraction - the classic error this question targets.

Related dot points

Sources & how we know this