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
intto adoublewhen 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
intis stored in 4 bytes, so values must fall betweenInteger.MIN_VALUEandInteger.MAX_VALUE; going past either causes integer overflow. - A
doublehas limited precision, so very precise results can have round-off error; usingintvalues 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.
</>Javaint 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.
</>Javaint 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_VALUEis the smallest possibleint.Integer.MAX_VALUEis the largest possibleint.
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.
</>Javaint 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.
</>Javaint 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:
</>Javadouble 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.
</>Javapublic 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:
</>Javaint 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:
</>Javadouble 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?
</>Javaint 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
</>Javaint 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.
</>Javapublic 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
doubleto anintdoes not round.(int)(3.9)is3, not4. 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 / 2and(int)(15.0 / 2)both give7, but not because they round. In the first, the cast turns15.0into15, so15 / 2is integer division. In the second,15.0 / 2is7.5and then gets truncated.- Storing a result in a
doubledoes not undo integer division. If both operands wereint, 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
intinto adoubleon its own, but converting adoubleto anintrequires an explicit cast. intanddoubleare the numeric primitive types in this course. Types likelong,float,short,byte, andcharare outside the scope here, so do not rely on them to avoid overflow on the exam.
Related AP Computer Science A Guides
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.