---
title: "AP CSA 1.5: Casting and Ranges of Variables"
description: "Review AP Computer Science A Topic 1.5, including casting, double to int truncation toward zero, automatic widening, integer overflow, Integer.MAX_VALUE, Integer.MIN_VALUE, and round-off error."
canonical: "https://fiveable.me/ap-comp-sci-a/unit-1/casting-and-ranges-of-variables/study-guide/kW3XXEIwJwVRXFx3ntdC"
type: "study-guide"
subject: "AP Computer Science A"
unit: "Unit 1 – Using Objects and Methods"
lastUpdated: "2026-06-09"
---

# AP CSA 1.5: Casting and Ranges of Variables

## Summary

Review AP Computer Science A Topic 1.5, including casting, double to int truncation toward zero, automatic widening, integer overflow, Integer.MAX_VALUE, Integer.MIN_VALUE, and round-off error.

## Guide

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](/ap-comp-sci-a/key-terms/expression "fv-autolink") that goes past `Integer.MAX_VALUE` or below `Integer.MIN_VALUE` causes overflow and produces an unexpected result. For [AP Computer Science A](/ap-comp-sci-a "fv-autolink"), 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](/ap-comp-sci-a/key-terms/arithmetic-expression "fv-autolink"). On the multiple-choice section, you trace code that mixes `int` and `double` math, and you decide whether [division](/ap-comp-sci-a/unit-1/expressions-and-assignment-statements/study-guide/01dr6uUPDAn3SjtK2Psr "fv-autolink") 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](/ap-comp-sci-a/key-terms/primitive-types "fv-autolink"); `(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](/ap-comp-sci-a/key-terms/round-off-error "fv-autolink"); 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](/ap-comp-sci-a/unit-4/wrapper-classes-integer-double/study-guide/CKbVZpoW1SH2jmwauzDE "fv-autolink"). 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](/ap-comp-sci-a/key-terms/integer-division "fv-autolink"). In the second line, `15.0 / 2` uses a `double`, so the division gives `7.5`, and then the cast truncates it. [Tracing](/ap-comp-sci-a/key-terms/tracing "fv-autolink") 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](/ap-comp-sci-a/key-terms/truncation "fv-autolink") 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](/ap-comp-sci-a/unit-3/abstraction-and-program-design/study-guide/o9VgVeIpKRYZ7N7rXfUz "fv-autolink") that returns the average of an [array](/ap-comp-sci-a/unit-4/array-creation-and-access/study-guide/umTe6NA38OqZOhMZjFWi "fv-autolink") 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](/ap-comp-sci-a/unit-3/scope-and-access/study-guide/56FUK4RSofr7slzwm6xm "fv-autolink") here, so do not rely on them to avoid overflow on the exam.

## Related AP Computer Science A Guides

- [1.4 Assignment Statements and Input](/ap-comp-sci-a/unit-1/14-assignment-statement-input/study-guide/compoundassignment)
- [1.13 Creating and Storing Objects](/ap-comp-sci-a/unit-1/creating-and-storing-objects/study-guide/rUOTKl6Ih5noXJ0GtxJF)
- [1.8 Documentation With Comments](/ap-comp-sci-a/unit-1/documentation-with-comments/study-guide/scrDad77j4e5vwrFab5J)
- [1.9 Calling a Void Method With Parameters](/ap-comp-sci-a/unit-1/calling-void-method-with-parameters/study-guide/QVWxxGeVjbIbLpC10d1Y)
- [1.6 Compound Assignment Operators](/ap-comp-sci-a/unit-1/compound-assignment-operators/study-guide/JiszeHqK8F9G6gszbgPL)
- [1.14 Calling a Void Method](/ap-comp-sci-a/unit-1/calling-a-void-method/study-guide/0RaM4GVOnbikS9dDp971)

## Vocabulary

- **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.
- **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.
- **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).

## FAQs

### 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.

## Structured Data

```json
{"@context":"https://schema.org","@type":"FAQPage","inLanguage":"en","mainEntity":[{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-1/casting-and-ranges-of-variables/study-guide/kW3XXEIwJwVRXFx3ntdC#what-is-casting-in-ap-computer-science-a","name":"What is casting in AP Computer Science A?","acceptedAnswer":{"@type":"Answer","text":"Casting forces Java to treat a primitive value as another primitive type, such as converting between int and double with (int) or (double)."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-1/casting-and-ranges-of-variables/study-guide/kW3XXEIwJwVRXFx3ntdC#does-casting-a-double-to-int-round-in-java","name":"Does casting a double to int round in Java?","acceptedAnswer":{"@type":"Answer","text":"No. Casting a double to an int truncates toward zero, so (int)3.9 becomes 3 and (int)(-3.9) becomes -3."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-1/casting-and-ranges-of-variables/study-guide/kW3XXEIwJwVRXFx3ntdC#what-is-automatic-widening-in-java","name":"What is automatic widening in Java?","acceptedAnswer":{"@type":"Answer","text":"Automatic widening happens when Java converts an int to a double without an explicit cast, usually because an expression needs a double value."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-1/casting-and-ranges-of-variables/study-guide/kW3XXEIwJwVRXFx3ntdC#why-does-integer-division-still-truncate-when-stored-in-a-double","name":"Why does integer division still truncate when stored in a double?","acceptedAnswer":{"@type":"Answer","text":"If both operands are int, Java performs integer division first. The truncated result is then widened if it is stored in a double variable."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-1/casting-and-ranges-of-variables/study-guide/kW3XXEIwJwVRXFx3ntdC#what-causes-integer-overflow-in-ap-csa","name":"What causes integer overflow in AP CSA?","acceptedAnswer":{"@type":"Answer","text":"Integer overflow happens when an int expression goes above Integer.MAX_VALUE or below Integer.MIN_VALUE. The result wraps to another int value."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-1/casting-and-ranges-of-variables/study-guide/kW3XXEIwJwVRXFx3ntdC#what-is-round-off-error-with-double-values","name":"What is round-off error with double values?","acceptedAnswer":{"@type":"Answer","text":"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."}}]}
```
