Cast to int in AP Computer Science A

In AP Computer Science A, casting to int means using the (int) operator to convert a value (usually a double) to an integer by truncating, or chopping off, everything after the decimal point. So (int) 4.8 is 4, not 5, because casting never rounds.

Verified for the 2027 AP Computer Science A examLast updated June 2026

What is Cast to int?

Casting to int is Java's way of forcing a value into the int type using the (int) cast operator. When you cast a double to an int, Java does not round. It truncates, meaning it throws away every digit to the right of the decimal point (EK 1.5.A.2). So (int) 9.99 is 9, and (int) -3.7 is -3, because truncation just deletes the decimal part for negatives too.

If you actually want the nearest integer, the CED gives you a specific trick: (int)(x + 0.5) for non-negative numbers and (int)(x - 0.5) for negative numbers (EK 1.5.A.4). Adding 0.5 before truncating pushes values like 4.6 up to 5.1, which truncates to 5. One more thing to keep straight: casting int to double is automatic in many situations (Java widens it for you), but going the other direction, double to int, always requires an explicit (int) cast because you're losing information.

Why Cast to int matters in AP® Computer Science A

This term lives in Topic 1.5 (Casting and Ranges of Variables) in Unit 1: Using Objects and Methods, under learning objective AP Comp Sci A 1.5.A, which asks you to cast primitive values in arithmetic expressions and determine the exact value produced. That last part is the whole game. The exam doesn't ask you to define casting; it gives you an expression like (int)(7.0 / 2) and expects you to trace it to a precise answer. Casting also connects directly to 1.5.B (integer overflow and the int range) and 1.5.C (round-off error in doubles), because choosing int vs. double is really a choice between exact-but-limited and flexible-but-imprecise. Truncation is one of the most reliable trap setups in Unit 1 multiple choice, so getting this cold is easy points.

How Cast to int connects across the course

Cast operator (Unit 1)

(int) is one of the two casting operators the CED names, alongside (double). They're mirror images. (double) widens an int so division keeps its decimal part, while (int) narrows a double and truncates it. Knowing which direction loses information tells you which cast must be written explicitly.

Arithmetic expressions and integer division (Unit 1)

Casting and integer division produce the same truncation behavior through different routes. 7 / 2 evaluates to 3 because both operands are ints, and (int)(7.0 / 2) evaluates to 3 because the cast truncates 3.5. MCQs love mixing these two so you have to track exactly when the decimal part disappears.

Round-off error (Unit 1)

EK 1.5.C.1 says doubles can't store every decimal exactly, so tiny round-off errors creep into double arithmetic. The CED's recommended fix is to use int values when you need exact results, which is why a payments program might cast dollar amounts to integer cents instead of comparing doubles with ==.

Integer overflow and Integer.MAX_VALUE (Unit 1)

Casting puts a value into int's world, and that world has hard walls. An int is stored in 4 bytes, so any result outside Integer.MIN_VALUE to Integer.MAX_VALUE overflows and wraps to a wrong-looking in-range value. Casting a huge double to int drops you into that same finite range.

Is Cast to int on the AP® Computer Science A exam?

Casting to int shows up in Unit 1 multiple choice as expression-evaluation questions where the answer choices are designed around the truncation trap. A classic setup sums doubles like 4.8 and 3.2 with total += (int) value inside a loop. If you truncate each value first you get 4 + 3 = 7, while the wrong answer 8 is waiting for anyone who added first or rounded. You should be able to (1) evaluate any cast expression to its exact value, (2) explain that casting truncates rather than rounds, (3) apply the (int)(x + 0.5) rounding trick, and (4) spot when an expression overflows the int range. Free-response questions rarely demand a cast by name, but casting can appear naturally whenever you convert a double calculation (an average, a percentage of a count) into an int result.

Cast to int vs Rounding to the nearest integer

Casting to int is not rounding. The (int) cast always truncates toward zero, so (int) 4.9 is 4 and (int) -4.9 is -4. Rounding finds the nearest integer, which on the AP exam you build manually with (int)(x + 0.5) for non-negative values or (int)(x - 0.5) for negative values. If a question's answer choices include both the truncated and the rounded result, the cast alone gives you the truncated one.

Key things to remember about Cast to int

  • Casting a double to int with the (int) operator truncates the value, meaning it deletes all digits after the decimal point instead of rounding.

  • Truncation works toward zero for negatives too, so (int) -3.7 evaluates to -3, not -4.

  • To round a double to the nearest integer, use (int)(x + 0.5) for non-negative numbers and (int)(x - 0.5) for negative numbers, exactly as stated in EK 1.5.A.4.

  • Java automatically widens int to double when needed, but converting double to int always requires an explicit (int) cast because information gets lost.

  • Once a value is an int, it must fit between Integer.MIN_VALUE and Integer.MAX_VALUE, and any expression that exceeds that range causes integer overflow.

  • The CED recommends using int values when you need exact arithmetic, since double values can suffer round-off errors.

Frequently asked questions about Cast to int

What does casting to int do in Java?

The (int) operator converts a value, usually a double, into an int by truncating everything after the decimal point. So (int) 7.9 evaluates to 7. This is EK 1.5.A.2 in the AP CSA CED.

Does casting to int round the number?

No. Casting truncates, it never rounds. (int) 4.999 is 4. To actually round on the AP exam, you write (int)(x + 0.5) for non-negative values or (int)(x - 0.5) for negative values.

What happens when you cast a negative double to int?

The decimal digits still just get chopped off, so the result moves toward zero. (int) -2.8 evaluates to -2, not -3. This trips people up because mathematical floor would give -3, but Java's cast is not floor.

How is casting to int different from integer division?

They both truncate, but at different moments. 7 / 2 truncates to 3 because both operands are ints, no cast needed. (int)(7.0 / 2) first computes 3.5 as a double, then the cast truncates it to 3. On MCQs, track the type of each operand to know when truncation happens.

Is casting to int on the AP Computer Science A exam?

Yes. It falls under Topic 1.5 and learning objective AP Comp Sci A 1.5.A, which requires you to evaluate cast expressions and determine the exact value produced. It's a frequent multiple-choice target, often combined with loops or ArrayList values.