AP CSA Casting Type Conversion

Unit 1 — Using Objects & Methods

Casting & Type Conversion New in 2026

Casting is one of the most-tested Unit 1 topics on the AP CSA exam. Questions on integer division, truncation, and forced floating-point division appear in nearly every exam. The 2025-2026 curriculum gives casting explicit treatment in section 1.5.

The Integer Division Trap

When both operands of / are int, Java performs integer division — the result is truncated to an integer. This is not a bug; it is designed behavior.

int a = 7;
int b = 2;
System.out.println(a / b);      // prints 3, not 3.5
System.out.println(7 / 2);      // prints 3
System.out.println(7.0 / 2);    // prints 3.5
System.out.println((double)7 / 2); // prints 3.5

Most common exam mistake: Computing an average as sum / count when both are int. The result is truncated. Fix: cast one operand to double first.

Explicit Casting Syntax

double x = 9.7;
int n = (int) x;        // n = 9 (truncated, not rounded)

int p = 5;
int q = 2;
double result = (double) p / q;  // 2.5 — cast p FIRST, then divide
double wrong  = (double)(p / q); // 2.0 — divides first, then casts result

Truncation vs Rounding

Casting double to int truncates (drops the decimal), it does NOT round. This applies to both positive and negative values.

Expression Result Explanation
(int) 3.9 3 Drops .9 — NOT rounded to 4
(int) 3.1 3 Drops .1
(int) -2.9 -2 Truncates toward zero, NOT -3
(int) -2.1 -2 Truncates toward zero
(int)(3.9 + 0.5) 4 Add 0.5 before casting to round

To round a double to the nearest int, add 0.5 before casting: (int)(x + 0.5). Or use Math.round(x) which returns a long.

Widening vs Narrowing Conversion

Widening (automatic — no cast needed)

Widening moves to a type that can hold all values of the original. No data is lost, so Java does it automatically.

int n = 42;
double d = n;     // widening: int automatically becomes double (42.0)
long L = n;       // widening: int to long (also automatic)

Narrowing (requires explicit cast)

Narrowing moves to a smaller type. Data may be lost, so Java requires an explicit cast to show you’re aware.

double d = 9.99;
int n = (int) d;  // narrowing: requires explicit cast, n = 9

double pi = 3.14159;
int truncated = (int) pi;   // truncated = 3

Casting with Math.random()

The standard pattern for generating a random integer in a range combines Math.random() (which returns a double in [0.0, 1.0)) with casting and scaling.

// Random int from 0 to 9 inclusive
int n = (int)(Math.random() * 10);

// Random int from 1 to 6 inclusive (die roll)
int die = (int)(Math.random() * 6) + 1;

// Random int from min to max inclusive
int rand = (int)(Math.random() * (max - min + 1)) + min;

The cast must wrap the entire product: (int)(Math.random() * 6). Writing (int)Math.random() * 6 first casts Math.random() to int (always 0), then multiplies — giving 0 every time.

Overflow and Integer Range

An int holds values from -2,147,483,648 to 2,147,483,647. If arithmetic produces a value outside this range, it wraps around without error. This is called integer overflow.

int max = Integer.MAX_VALUE;   // 2147483647
System.out.println(max + 1);  // -2147483648 (overflow! wraps to MIN_VALUE)

Practice MCQs

What is the value of result after the following executes?

int a = 10;
int b = 3;
double result = (double)(a / b);

  • (A) 3.3333...
  • (B) 3.0
  • (C) 3
  • (D) A compile error occurs.

What is printed by the following?

System.out.println((int)(-3.8));

  • (A) -4
  • (B) -3
  • (C) 4
  • (D) 3

Which expression produces a random int from 1 to 10 inclusive?

  • (A) (int)(Math.random() * 10)
  • (B) (int)(Math.random() * 10) + 1
  • (C) (int)Math.random() * 10 + 1
  • (D) (int)(Math.random() * 11)

An ArrayList called scores contains several values. Consider:

int total = 0;
for (int s : scores) total += s;
double avg = total / scores.size();

Which best describes the issue with computing avg?

  • (A) scores.size() returns a double, causing a compile error.
  • (B) total / scores.size() performs integer division, truncating the decimal.
  • (C) The enhanced for loop incorrectly unboxes Integer to int.
  • (D) total must be declared as long to avoid overflow.

Common Mistakes

  • Integer division in averages: sum / count when both are int truncates. Cast first: (double) sum / count.
  • Casting after division: (double)(sum/count) casts the truncated integer, not the true average.
  • Thinking truncation rounds: (int)3.9 is 3, not 4. Add 0.5 before casting to round.
  • Casting Math.random() too early: (int)Math.random() * N casts Math.random() first (always 0). Always wrap the full product: (int)(Math.random() * N).
  • Confusion with negative truncation: (int)(-2.9) = -2, not -3. Truncation is toward zero.

Related Topics

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]