Fiveable

💻AP Computer Science A Unit 1 Review

QR code for AP Computer Science A practice questions

1.5 Casting and Ranges of Variables

1.5 Casting and Ranges of Variables

Written by the Fiveable Content Team • Last updated June 2026
Verified for the 2027 exam
Verified for the 2027 examWritten by the Fiveable Content Team • Last updated June 2026
💻AP Computer Science A
Unit & Topic Study Guides

Frequently Asked Questions

Previous Exam Prep

Study Tools

Exam Skills

AP Cram Sessions 2021

Pep mascot

Casting lets you convert between int and double, but converting a double to an int truncates the decimal part instead of rounding. Integer values also have a fixed range, so an expression that goes past Integer.MAX_VALUE or below Integer.MIN_VALUE causes overflow and produces an unexpected result. For AP Computer Science A, trace the type of each value before deciding what an expression evaluates to.

Why This Matters for the AP Computer Science A Exam

Casting and ranges show up whenever you need to predict the value of an arithmetic expression. On the multiple-choice section, you trace code that mixes int and double math, and you decide whether division truncates or keeps the decimal. On the free-response code-writing section, calculating an average or converting between types correctly often depends on placing a cast in the right spot. This topic also builds the habit of asking whether both operands are integers before you trust a result, which carries through every later unit.

Key Takeaways

  • The cast operators (int) and (double) convert between primitive types; (int) truncates the decimal part instead of rounding.
  • Java automatically widens an int to a double when an expression needs it, but it never narrows automatically.
  • To round a double, use (int)(x + 0.5) for non-negative numbers and (int)(x - 0.5) for negative numbers.
  • An int is stored in 4 bytes, so values must fall between Integer.MIN_VALUE and Integer.MAX_VALUE; going past either causes integer overflow.
  • A double has limited precision, so very precise results can have round-off error; using int values avoids that.
  • Parentheses control whether you cast before or after dividing, which changes the result.

Casting Primitive Values

Casting forces Java to treat a value as a different primitive type. The syntax puts the target type in parentheses: (int) or (double).

The key rule: casting a double to an int truncates, meaning it drops everything after the decimal point. It does not round.

</>Java
// Casting truncates, it never rounds
(int)(3.1)    // 3
(int)(3.9)    // 3 (not 4)
(int)(-3.1)   // -3
(int)(-3.9)   // -3 (not -4)

Where you put the parentheses changes the result, because a cast only applies to the value immediately after it unless you group with parentheses.

</>Java
// (int) applies to 15.0 first, then division happens
(int) 15.0 / 2    // 7: 15.0 becomes 15, then 15 / 2 is integer division = 7

// The division happens first, then the result is cast
(int)(15.0 / 2)   // 7: 15.0 / 2 is 7.5, then (int) truncates to 7

Both of these produce 7, but for different reasons. In the first line, casting 15.0 to 15 leaves you with two int values, so 15 / 2 is integer division. In the second line, 15.0 / 2 uses a double, so the division gives 7.5, and then the cast truncates it. Tracing each step in order is what keeps you from guessing.

Automatic Widening

Java automatically converts an int to a double when an expression needs a double. This is called widening, and it does not lose information. Java will not automatically go the other direction.

</>Java
int x = 5;
double y = x;        // automatic widening, y = 5.0
double z = x / 2;    // x / 2 is integer division (2), then widened to 2.0
double w = x / 2.0;  // x is widened first, then 5.0 / 2.0 = 2.5

Notice the difference between z and w. With x / 2, both operands are int, so the division truncates to 2 before anything is widened. With x / 2.0, one operand is a double, so the whole expression evaluates as double and you get 2.5.

Rounding a double

Since casting truncates, you need a small trick to actually round. For a non-negative number, add 0.5 before casting. For a negative number, subtract 0.5 before casting.

</>Java
int roundedUp   = (int)(positiveNum + 0.5);  // rounds non-negative values
int roundedDown = (int)(negativeNum - 0.5);  // rounds negative values

Adding 0.5 pushes a value like 3.6 up to 4.1, so truncating gives 4. For negatives, subtracting 0.5 pushes the value far enough that truncation lands on the correct rounded integer.

Integer Range and Overflow

Integers are stored in a fixed 4 bytes of memory, so they cannot be infinitely large. Every int must fall between two constants:

  • Integer.MIN_VALUE is the smallest possible int.
  • Integer.MAX_VALUE is the largest possible int.

If an expression would produce an int outside that range, integer overflow happens. The result is still a valid int, but not the value you expected. Adding 1 to Integer.MAX_VALUE wraps around to Integer.MIN_VALUE.

</>Java
int big = Integer.MAX_VALUE;
System.out.println(big);      // largest int
big = big + 1;
System.out.println(big);      // wraps around to the smallest int

Java does not warn you when overflow happens; the code just keeps running with the wrong value. On the exam, you mostly need to recognize when an expression could overflow, not prevent it.

Round-Off Error with double

A double is stored in a fixed amount of memory too, so it can only hold so much precision. If a result is more precise than what fits, the value is rounded to the nearest representable value, which is called round-off error. When you need exact whole-number results, using int values avoids this.

How to Use This on the AP Computer Science A Exam

Code Tracing

When you trace an expression on the multiple-choice section, ask one question first: are both operands int? If yes, the division truncates and the result is an int, even if it later gets stored in a double.

</>Java
int x = 15;
int y = 2;
double z = (x + 1) / y;   // (15 + 1) / 2 = 16 / 2 = 8, stored as 8.0

The answer is 8.0, not 8.5. Both operands are int, so the division happens in integer math, and the 8 is widened to 8.0 only when it is stored.

For casting questions, track exactly what the cast touches:

</>Java
double result1 = (double)(3 + 4) / 2;   // (3 + 4) becomes 7.0, then 7.0 / 2 = 3.5
double result2 = (double)3 + 4 / 2;      // 3.0 + (4 / 2) = 3.0 + 2 = 5.0

In result2, the cast only applies to 3, and 4 / 2 is still integer division.

Free Response

When you write code that calculates an average or a percentage, you usually receive int inputs but need a double result. Place a cast so the division uses a double.

</>Java
public double calculateAverage(int score1, int score2) {
    // Cast so division is not integer division
    return (score1 + score2) / 2.0;
}

Without the 2.0 or a cast, (89 + 90) / 2 would truncate to 89 and then store 89.0, losing the .5.

Common Trap

Watch percentage calculations where every value is an int:

</>Java
int correct = 17;
int total = 20;
double percent = correct / total * 100;   // 0.0, not 85.0

Here 17 / 20 is integer division and gives 0, so 0 * 100 is 0. Fix it by making one operand a double before the division:

</>Java
double percent = (double)correct / total * 100;   // 85.0
// or
double percent = correct * 100.0 / total;         // 85.0

Practice Problems

Problem 1: Casting Order

What does this code output?

</>Java
int a = 7;
int b = 2;
System.out.println((double)(a / b));
System.out.println((double)a / b);

The first line outputs 3.0. The division a / b happens first as integer division (7 / 2 = 3), then 3 is cast to 3.0. The second line outputs 3.5, because a is cast to 7.0 first, so 7.0 / 2 = 3.5.

Problem 2: Spotting Overflow

</>Java
int x = Integer.MAX_VALUE - 2;

for (int i = 0; i < 5; i++) {
    System.out.println("x = " + x);
    x++;
}

The values climb until x reaches Integer.MAX_VALUE. The next x++ overflows and wraps around to Integer.MIN_VALUE, then continues from there. This is a good reminder that incrementing past the maximum does not produce a larger number.

Problem 3: Correct Average

Write a method that returns the average of an array of integers as a double.

</>Java
public double findAverage(int[] nums) {
    int sum = 0;
    for (int num : nums) {
        sum += num;
    }
    return (double)sum / nums.length;   // cast so division keeps the decimal
}

The cast on sum makes the division a double operation, so the average keeps its decimal part instead of truncating.

Common Misconceptions

  • Casting a double to an int does not round. (int)(3.9) is 3, not 4. Use (int)(x + 0.5) for non-negative values or (int)(x - 0.5) for negative values when you actually want rounding.
  • (int) 15.0 / 2 and (int)(15.0 / 2) both give 7, but not because they round. In the first, the cast turns 15.0 into 15, so 15 / 2 is integer division. In the second, 15.0 / 2 is 7.5 and then gets truncated.
  • Storing a result in a double does not undo integer division. If both operands were int, the truncation already happened before the value was widened.
  • Overflow does not crash the program or throw an error. The value just wraps around to the other end of the range and the code keeps running with the wrong number.
  • Widening is automatic, but narrowing is not. Java will turn an int into a double on its own, but converting a double to an int requires an explicit cast.
  • int and double are the numeric primitive types in this course. Types like long, float, short, byte, and char are outside the scope here, so do not rely on them to avoid overflow on the exam.

Vocabulary

The following words are mentioned explicitly in the College Board Course and Exam Description for this topic.

Term

Definition

arithmetic expression

Mathematical expressions consisting of numeric values, variables, and operators that perform calculations.

casting

The process of converting a value from one primitive data type to another, such as from double to int or int to double.

casting operator

Operators used to convert a value from one data type to another, such as converting a double to an int.

data type

A classification that specifies what kind of value a variable can store and what operations can be performed on it.

double

A primitive data type used to store real numbers (numbers with decimal points).

int

A primitive data type used to store integer values (whole numbers).

integer expression

A mathematical or computational expression that evaluates to an integer value.

integer overflow

An error that occurs when an integer expression evaluates to a value outside the allowed range for the int data type, resulting in an unexpected value.

Integer.MAX_VALUE

A Java constant that holds the largest possible value that can be stored in an int variable.

Integer.MIN_VALUE

A Java constant that holds the smallest possible value that can be stored in an int variable.

out of range

A value that exceeds the maximum or minimum limits allowed for a particular data type.

precision

The degree of exactness with which a floating-point number can be represented in computer memory.

primitive data type

Basic data types in Java such as int, double, boolean, and char that are not objects.

round-off error

An error that occurs when a computed value cannot be exactly represented in the available memory and must be rounded to the nearest representable value.

rounding

The process of converting a decimal number to the nearest integer value.

truncation

The removal of digits to the right of the decimal point when casting a double value to an int value.

widening

The automatic conversion of a value from a smaller primitive type (like int) to a larger primitive type (like double).

Frequently Asked Questions

What is casting in AP Computer Science A?

Casting forces Java to treat a primitive value as another primitive type, such as converting between int and double with (int) or (double).

Does casting a double to int round in Java?

No. Casting a double to an int truncates toward zero, so (int)3.9 becomes 3 and (int)(-3.9) becomes -3.

What is automatic widening in Java?

Automatic widening happens when Java converts an int to a double without an explicit cast, usually because an expression needs a double value.

Why does integer division still truncate when stored in a double?

If both operands are int, Java performs integer division first. The truncated result is then widened if it is stored in a double variable.

What causes integer overflow in AP CSA?

Integer overflow happens when an int expression goes above Integer.MAX_VALUE or below Integer.MIN_VALUE. The result wraps to another int value.

What is round-off error with double values?

Round-off error happens when a double result is more precise than Java can store exactly, so the value is rounded to the nearest representable value.

Pep mascot
Upgrade your Fiveable account to print any study guide

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Click below to go to billing portal → update your plan → choose Yearly→ and select "Fiveable Share Plan". Only pay the difference

Plan is open to all students, teachers, parents, etc
Pep mascot
Upgrade your Fiveable account to export vocabulary

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Plan is open to all students, teachers, parents, etc
report an error
description

screenshots help us find and fix the issue faster (optional)

add screenshot