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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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.9is3(not4- it does not round).(int) -3.9is-3(toward zero).(double) 5is5.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 tointtruncates 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 / 2is integer division (4) before the cast, so casting gives4.0. In the second,9is cast to9.0first, so9.0 / 2is floating-point division giving4.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
- Topic 1.3 Expressions and Assignment Statements: evaluate arithmetic expressions using operator precedence, integer division and the modulo operator, and assign results to variables.
A focused answer to AP CSA Topic 1.3, covering arithmetic operators, operator precedence, integer division truncation, the modulo operator, and how assignment statements store results, with a fully traced worked evaluation.
- Topic 1.2 Variables and Data Types: identify the primitive types int, double and boolean, and declare, initialise and use variables of those types.
A focused answer to AP CSA Topic 1.2, covering the difference between primitive and reference types, the three exam primitives int, double and boolean, declaring and initialising variables, valid identifiers, and constants, with a traced worked example.
- Topic 1.4 Compound Assignment Operators: use the compound assignment operators (+=, -=, *=, /=, %=) and the increment and decrement operators (++, --) as shorthand to update variables.
A focused answer to AP CSA Topic 1.4, covering the compound assignment operators, the increment and decrement operators, how each rewrites a longer assignment, and the integer-division traps they can hide, with a traced worked example.
- Topic 2.9 Using the Math Class: call the static Math methods in the AP Java subset (abs, pow, sqrt, random) and generate a random integer or double in a specified range.
A focused answer to AP CSA Topic 2.9, covering the required static Math methods abs, pow, sqrt and random, why Math methods are called on the class, the return types, and the standard formula for generating a random int in a range, with a worked trace.
- Topic 2.8 Wrapper Classes: Integer and Double: use the Integer and Double wrapper classes, including autoboxing and unboxing, the MIN_VALUE and MAX_VALUE constants, and parsing methods.
A focused answer to AP CSA Topic 2.8, covering why wrapper classes exist, creating Integer and Double objects, autoboxing and unboxing, Integer.MIN_VALUE and MAX_VALUE, parseInt and parseDouble, and the == versus equals trap for wrappers, with a worked trace.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)