01 / 22
int double

Casting & Range
of Variables

Master how Java converts between primitive types and understand the storage limits that every variable carries.

AP Computer Science A

Java's Primitive Types

Every variable in Java must have a declared type. The AP exam focuses on three primitive types, each storing a different kind and size of data.

int
32-bit signed integer. Stores whole numbers from \(-2^{31}\) to \(2^{31}-1\).
double
64-bit floating point. Stores decimal numbers with about 15 digits of precision.
boolean
Stores exactly two values: true or false. Cannot be cast to a number.

Range of int

-2,147,483,648 MIN_VALUE 0 2,147,483,647 MAX_VALUE

Range of double

\(-1.8 \times 10^{308}\) 0.0 \(1.8 \times 10^{308}\) ~15 digits of precision

What Is Casting?

Casting is the process of converting a value from one data type to another. Java has two kinds:

Widening (Implicit)
Automatic: a smaller type fits safely into a larger type. No data is lost. Example: int to double.
Narrowing (Explicit)
Manual: a larger type is forced into a smaller type. Data may be lost. Example: double to int.

Widening Cast: int to double

int x = 42; 42 automatic double y = x; 42.0 32 bits 64 bits

Narrowing Cast: double to int

double d = 9.78; 9.78 (int) cast int n = (int) d; 9 .78 is truncated

Truncation: Toward Zero

When casting from double to int, Java truncates toward zero. This means it chops off the decimal, regardless of whether the value is positive or negative:

(int) 3.99
Result: 3
Not rounded up to 4
(int) -3.99
Result: -3
Not rounded down to -4
(int) 0.5
Result: 0
Even 0.999 becomes 0

Integer Division and Casting

int a = 7; int b = 2; double result1 = a / b; // 3.0, NOT 3.5 double result2 = (double) a / b; // 3.5

Widening vs. Narrowing

Widening (Implicit) Narrowing (Explicit)
Direction int → double double → int
Operator needed? No Yes: (int)
Data loss? No (value preserved) Yes (decimal truncated)
Compiler error if omitted? N/A Yes
Example double d = 5; int n = (int) 5.9;

Integer Overflow

When an arithmetic operation produces a value outside the range of int, the result wraps around silently: no error is thrown.

int big = 2147483647; // Integer.MAX_VALUE big = big + 1; System.out.println(big); // -2147483648 (wraps!)

How to Apply Casting

1
Identify the types. What type is the expression? What type is the variable receiving it?
2
Check the direction. Small to large (widening) is automatic. Large to small (narrowing) requires an explicit cast.
3
Watch for integer division. If both operands are int, the result is int even if stored in a double.
4
Remember truncation. Casting to int always truncates toward zero: it never rounds.
5
Check for overflow. Will the result fit in the target type's range?

Example 1: Simple Cast

double temp = 98.6; int rounded = (int) temp; System.out.println(rounded); // ?

Example 2: Division Trap

int total = 17; int count = 5; double avg = total / count; System.out.println(avg); // ?
// Fix: cast one operand before dividing double avg = (double) total / count; // 3.4

Example 3: Mixed Expressions

int x = 5; double y = 2.5; double result = x + y; // 7.5 int z = (int) (x + y); // 7

Common Mistakes

✕ Assuming Rounding
Casting (int) 2.9 gives 2, not 3. Truncation is not rounding.
✕ Forgetting Integer Division
Writing int/int and expecting a decimal result. Both sides must be checked.
✕ Ignoring Overflow
Adding large ints without checking if the result exceeds \(2^{31}-1\). Java wraps silently.

Summary Table

Concept Key Rule Watch Out
int range \(-2^{31}\) to \(2^{31}-1\) Overflow wraps silently
double range \(\pm 1.8 \times 10^{308}\) ~15 digits of precision
Widening Automatic (int to double) No data loss
Narrowing Requires (int) cast Truncates, does not round
Integer division int / int = int Cast before dividing

Question 1 of 5

What is the output of the following code?
System.out.println((int) 7.9);

Question 2 of 5

What value does result hold after this code runs?
double result = 11 / 4;

Question 3 of 5

Which of the following requires an explicit cast to compile?

Question 4 of 5

What is printed by this code?
System.out.println((int) -4.7);

Question 5 of 5

What happens when you add 1 to Integer.MAX_VALUE in Java?