Topic 1.5 Notes – Casting and Range of Variables
1. Casting Between int and double
Casting means you explicitly convert one primitive numeric type to another using (int) or (double). In this unit, it’s only about int and double.
(int)converts adouble → int(double)converts anint → double- The cast applies only to the value immediately to its right, unless you use parentheses
Here’s the big idea: converting down (double to int) can lose information. Converting up (int to double) does not.
Double to int Always Truncates
When you cast a double to an int, Java cuts off the decimal part. It does not round.
(int) 4.8→4(int) -4.8→-4
It always truncates toward zero, as shown on the number line below.

Casting double to int truncates toward 0
This shows up constantly in multiple choice. If you see (int) on a decimal, assume the decimal disappears.
Int to double
Casting an int to a double just adds .0.
(double) 6→6.0
No data is lost because double can represent all int values exactly within range.
Parentheses and Order Matter
Casting has high precedence. It only affects what comes immediately after it.
Compare:
int a = 9;
int b = 4;
System.out.println((double)(a / b));
System.out.println((double)a / b);
Trace it:
a / b→9 / 4→2(integer division)- First line prints
2.0 - Second line casts
afirst →9.0 / 4→2.25
Before answering, ask yourself:
- Does division happen before casting?
- Are both operands still
intat that moment?
That mental check prevents most mistakes.
2. Automatic Widening and Integer Division
Java will automatically convert int → double when needed. This is called widening.
Java will not automatically convert double → int. That requires a cast because information could be lost.
Widening in Expressions
If either operand in an arithmetic expression is a double, the result is a double.
3 + 2.0→5.07 / 2.0→3.5
Integer Division Happens First
If both operands are int, division is integer division, even if the result is stored in a double.
double x = 11 / 4;
Step-by-step:
11 / 4→2- Then widened to
2.0
To force decimal division, make at least one operand a double:
double x = 11 / 4.0;
double y = (double)11 / 4;
double z = (double)(a + b) / n;
Students often write:
(double)(a / b)
If a and b are int, it’s too late. The truncation already happened.
This pattern appears all the time in average and percentage problems.
3. Rounding a double to the Nearest int
Since casting truncates, you must adjust the value before casting to round.
For non-negative numbers:
(int)(x + 0.5)
For negative numbers:
(int)(x - 0.5)
Why subtract for negatives? Because truncation moves toward zero.
Example:
x = -2.6x - 0.5 = -3.1(int)(-3.1)→-3
You must adjust first, then cast.
4. Range of int and Integer Overflow
An int uses 4 bytes (32 bits) of memory. That means it has a fixed range:
Integer.MAX_VALUE = 2147483647Integer.MIN_VALUE = -2147483648
All int values must stay within that range.
On a number line, int values run from Integer.MIN_VALUE on the left to Integer.MAX_VALUE on the right. When you go past the maximum, the value wraps around to the minimum.

int overflow wraparound
What Causes Overflow
Overflow happens when a calculation produces a value outside the allowed range.
Examples:
Integer.MAX_VALUE + 1- Large repeated multiplication
- Big accumulators in loops
What Actually Happens
Java does not give an error.
It wraps around.
If you add 1 to Integer.MAX_VALUE, the result becomes Integer.MIN_VALUE.
The result is still an int, just not the mathematically correct value. On the AP exam, they usually want you to recognize that overflow will occur and that Java will silently wrap.
5. Limits of Precision with double
A double also has limited memory. It cannot store infinite decimal precision.
Some decimals cannot be represented exactly in binary. When a calculation needs more precision than available, a round-off error occurs. The value is rounded to the nearest representable number.
This matters when:
- Repeatedly adding decimals
- Comparing doubles for exact equality
- Accumulating fractional results
If exact precision is required, use int when possible. Special decimal classes are outside AP scope.