Math, Wrapper Classes, Casting and Overflow

AP Computer Science A · Unit 1 — Using Objects and Methods

Math, wrapper classes, casting, and the edge of int

Math is a class of static methods, so you call them on the class name. The four in the AP subset are Math.abs, Math.pow, Math.sqrt and Math.random. Math.pow and Math.sqrt always return a double, even for Math.pow(2, 3), which prints as 8.0.

Math.random() returns a double in the interval from 0 (included) to 1 (excluded). The pattern for a random integer from low to high inclusive is:

(int) (Math.random() * (high - low + 1)) + low

The + 1 is there because the count of integers from low to high inclusive is high - low + 1.

Casting with (int) truncates toward zero; it does not round. It also binds tightly, so (int) 7.9 / 2 is 7 / 2 = 3, while (int) (7.9 / 2) is (int) 3.95 = 3 as well but for a different reason — change the numbers and the two differ. Integer division discards the remainder, so 1 / 2 * 3.0 is 0.0.

Wrapper classes Integer and Double let a primitive be treated as an object, and they carry the constants Integer.MAX_VALUE and Integer.MIN_VALUE. Arithmetic that passes those limits does not throw an exception: it wraps around silently.

MIN_VALUE−2147483648 0 MAX_VALUE2147483647 MAX_VALUE + 1 wraps to MIN_VALUE, with no error 2000000000 + 2000000000 exceeds the limit → prints −294967296 Prevent it by casting to long BEFORE computing: (long) a + b
Why the number comes out this way. The int type has exactly 2^32 = 4294967296 values arranged in a circle. Adding past the right end continues from the left end, so the actual result equals the mathematical result minus 4294967296: 4000000000 - 4294967296 = -294967296. The danger is that no exception is thrown — the program keeps running with a nonsensical negative number. To prevent it, you must cast to long before the operation; casting after the calculation is already too late.

Worked example: four lines, three classic errors

Determine the value printed by each line and explain where students are most likely to slip.

System.out.println(Math.pow(3, 2) + " " + (int) Math.pow(3, 2));
System.out.println((int) (3.7 + 3.7) + " " + ((int) 3.7 + (int) 3.7));
System.out.println(7 / 2 + " " + 7 / 2.0 + " " + (double) (7 / 2));
int n = Integer.MAX_VALUE;
System.out.println(n + 1);

Solution

Line 1. Math.pow always returns a double, so the first part prints 9.0; with the (int) cast it becomes 9. Result: 9.0 9.

Line 2. The first part adds first and truncates afterward: (int) 7.4 gives 7. The second part truncates each number first and then adds: 3 + 3 gives 6. Result: 7 6. The two parts differ by 1 because the decimal part is discarded twice.

Line 3. 7 / 2 is integer division, so it gives 3. 7 / 2.0 has one real operand, so it gives 3.5. And (double) (7 / 2) divides as integers first and only then widens the type, so it gives 3.0 — widening late cannot recover what was already lost. Result: 3 3.5 3.0.

Line 4. It exceeds the limit and wraps around, printing -2147483648. No exception is thrown.

These four lines capture the three most common ways to lose points: forgetting that Math returns a double, truncating before adding, and widening the type after the integer division has already happened.

Common mistake

Casting too late. double tb = (double) (tong / soCau); looks perfectly reasonable, but it finishes the integer division before widening the type, so an average of 8.75 shows up as 8.0. You must write (double) tong / soCau. In the same family is overflow: when an int goes past 2147483647 it silently wraps around, and the only way to prevent it is to cast one operand to (long) before adding or multiplying. And remember that (int) truncates rather than rounds — to round, use Math.round, or add 0.5 and then truncate (for positive numbers).

Key takeaway

Math.pow and Math.sqrt return a double. (int) truncates toward 0, does not round, and binds tightly to the operand right after it. Widening the type must happen before the division. int overflow wraps around silently; prevent it with (long) placed before the operation.

← The String Class and Immutability · All AP Computer Science A lessons · Practice AP Computer Science A questions (free, instant scoring) · Boolean Expressions and De Morgan's Laws →