Topic 1.11 Notes – Math Class
1. What the Math Class Is and How You Use It
Math is part of the java.lang package, which means it is automatically available. No import statement needed.
Every method in Math is static, so you call it using the class name:
double result = Math.sqrt(36);
You never create a Math object.
- ✔️
Math.abs(x) - ❌
new Math() - ❌
x.sqrt()
These methods return values. They do not print anything and they do not change a variable unless you assign the result.
When reading code on a quiz, always ask:
- What type does this method return?
- What exact value is produced?
- Is there any casting happening?
2. The Math Methods You Must Know
These are the only Math methods you’re responsible for on the AP exam.
Math.abs()
Two versions exist:
int abs(int x)double abs(double x)
It returns the absolute value (distance from 0).
Math.abs(-9); // 9
Math.abs(-2.5); // 2.5
Common use: finding differences.
int diff = Math.abs(a - b);
Return type depends on the parameter type.
Math.pow(base, exponent)
double pow(double base, double exponent)
Raises base to a power.
It always returns a double, even if both inputs are integers.
Math.pow(3, 2); // 9.0
Math.pow(9, 0.5); // 3.0
If you want to store it in an int, you must cast:
int x = (int) Math.pow(3, 2);
No cast → compile error.
Math.sqrt()
double sqrt(double x)
Returns the nonnegative square root.
Math.sqrt(49); // 7.0
Always returns double.
This shows up in geometry-style problems or distance calculations.
Math.random()
double random()
Returns a value in:
[0.0, 1.0)
On a number line, that looks like this:

Range of values returned by Math.random()
That means:
- 0.0 is possible
- 1.0 is NOT possible
This detail drives all random range questions.
3. Generating Random Numbers in a Range
This is where most points are lost.
Random integer from 0 to n − 1
int r = (int)(Math.random() * n);
Why it works:
Math.random()→ [0.0, 1.0)- Multiply by
n→ [0.0, n) - Cast to int → 0 to n − 1
Casting truncates. It does not round.
Random integer from min to max (inclusive)
This is the most important formula:
int r = (int)(Math.random() * (max - min + 1)) + min;
Why +1?
Because you want the upper bound included.
Walk through the transformation step by step:

Transforming [0.0, 1.0) into an inclusive integer range
If you forget +1, the maximum value will never appear. That mistake shows up constantly on MCQs.
Random double in a range
For a double in [min, max):
double r = Math.random() * (max - min) + min;
No casting needed.
4. Return Types and Casting Rules
Keep this straight:
| Method | Return Type |
|---|---|
abs(int) | int |
abs(double) | double |
pow() | double |
sqrt() | double |
random() | double |
Casting example:
int x = (int) Math.sqrt(20);
If Math.sqrt(20) is about 4.47, casting gives 4.
Casting truncates toward zero.
This detail matters in tracing questions.
5. Common Mistakes You Should Catch Instantly
- Forgetting that
Math.random()never returns 1.0 - Forgetting
+1when the range is inclusive - Trying to store
Math.pow()in anintwithout casting - Calling methods like
x.abs()instead ofMath.abs(x) - Assuming casting rounds instead of truncates
When you see a random expression on a test, rewrite the interval after every step. That habit alone prevents most errors.