The Math class: ready-made numeric tools.
When you need an absolute value, a power, a square root, or a random number, the Math class has it covered.
The Math class is part of the standard Java library. Every method in it is static — you call it on the class itself, never on an object. You do not need to import it; it's available everywhere.
The AP CSA exam tests a focused subset of Math methods: abs, pow, sqrt, and random. Knowing these four — and one classic formula built from random — covers almost every appearance on the exam.
Math methods are tested in MCQ tracing, in expression-evaluation problems, and as components inside larger FRQ solutions.
Four static methods you must know cold.
Plus the random-integer formula built on top of Math.random().
Math.abs
Math.abs(-7); // 7 (int)
Math.abs(-3.5); // 3.5 (double)
Returns the absolute value. The return type matches the argument type.
Math.pow
double p = Math.pow(2, 10); // 1024.0
Returns base raised to the exponent as a double. Even when the result is a whole number, it comes back as a double.
Math.sqrt
double r = Math.sqrt(16); // 4.0
Returns the non-negative square root as a double.
Math.random
double d = Math.random(); // 0.0 <= d < 1.0
Returns a random double in the range [0.0, 1.0) — inclusive of 0, exclusive of 1.
Random integer in a range
The standard AP formula for a random integer from low to high inclusive:
int rand = (int)(Math.random() * (high - low + 1)) + low;
The (int) cast truncates the decimal. Multiplying by high - low + 1 stretches the range. Adding low shifts it.
Casts and Math.pow
int n = (int) Math.pow(2, 5); // 32
Because Math.pow returns a double, you must cast to int when storing the result in an int.
Math-class words you must know.
Used in MCQ stems and FRQ rubrics throughout AP CSA.
- Static method — called on the class, not on an object.
- Math class — Java's built-in mathematical utility class.
- Absolute value — non-negative magnitude of a number.
- Cast — convert a value from one type to another, like
(int) d. - Truncation — cutting off the decimal part when casting
doubletoint. - Inclusive / exclusive range — describes whether endpoints are included.
How the Math class is tested.
Return-type subtleties and the random-integer formula dominate.
MCQ patterns to expect:
- Return type confusion.
Math.powandMath.sqrtreturndouble— always. - Casting traps.
(int) Math.pow(2, 3)gives8; without the cast, it stays adouble. - Random range formula. Expect a question that asks the range of
(int)(Math.random() * n) + k. - abs sign.
Math.abs(0)is0; it never returns a negative.
FRQ tip: when a problem asks for a random integer in a range, write the formula carefully and verify the bounds with the endpoints before moving on.
Build a random integer between 5 and 10 inclusive.
Apply the AP CSA random-integer formula step by step.
Problem. Write an expression that produces a random integer in the range 5 to 10, inclusive.
Step 1. Count the values in the range. From 5 to 10 inclusive is 6 values: 5, 6, 7, 8, 9, 10. This is high - low + 1 = 10 - 5 + 1 = 6.
Step 2. Math.random() returns a double in [0.0, 1.0). Multiply by 6 to get a double in [0.0, 6.0).
Step 3. Cast to int. The cast truncates, producing an integer in {0, 1, 2, 3, 4, 5}.
Step 4. Add 5 to shift the range to {5, 6, 7, 8, 9, 10}.
int r = (int)(Math.random() * 6) + 5;
Answer. The expression (int)(Math.random() * 6) + 5 produces a uniform random integer from 5 to 10 inclusive.
Why this matters. This formula appears on nearly every AP CSA exam. Memorize it as (int)(Math.random() * range) + low with range = high - low + 1.
Errors that lose easy Math-class points.
Small details cause big point losses.
- Calling on a number.
x.pow(2)is wrong; useMath.pow(x, 2). - Forgetting the cast.
int n = Math.pow(2, 3);won't compile; cast with(int). - Wrong random range. Many students forget the
+ 1when computing inclusive ranges. - Assuming
Math.random()can return 1.0. It can't — the upper bound is exclusive. - Using
**. Java has no**operator for exponents; useMath.pow. - Treating
Math.sqrtasint. It always returns adouble.
Math class quick reference.
Exactly the methods listed in the AP CSA Java Quick Reference.
Math Class Reference
AP Quick Reference| Method | Returns | AP Exam Tip |
|---|---|---|
Math.abs(int x) |
int absolute value |
Return type matches argument type. |
Math.abs(double x) |
double absolute value |
Never returns a negative. |
Math.pow(double base, double exp) |
double result |
Cast to int if you need a whole number. |
Math.sqrt(double x) |
double non-negative root |
Argument should be non-negative. |
Math.random() |
double in [0.0, 1.0) |
Never returns 1.0. |
Random int low to high |
int in inclusive range |
(int)(Math.random() * (high - low + 1)) + low |
How to master the Math class.
Memorize four methods and one formula. That's the entire topic.
Use the three practice tools below this lesson in order:
- MCQ Practice — for every expression, identify the return type first. Then check whether a cast is needed. Then evaluate.
- Java Lab — practise calling each method with various arguments. Always cast
Math.powandMath.sqrtresults when assigning toint. - FRQ Practice — use the random-integer formula when a problem calls for random behavior. Verify the bounds match the inclusive range stated in the problem.
If you can write the random-integer formula from memory, you've already mastered the most-tested part of this topic.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.