5m left·0%
Reading Time: 5 min
Last Updated: July 2, 2026
Main Ideas: 5
Reading Time: 5 min
Last Updated: July 2, 2026
Main Ideas: 5

Topic 1.11 Notes – Math Class

Verified for 2027 AP® Computer Science A Exam
Read aloud
The Math class in Java gives you built-in mathematical operations like absolute value, powers, square roots, and random number generation. All of its methods are static and automatically available, so you call them directly using Math.methodName(...). On the AP exam, you’ll need to evaluate these expressions precisely, including return types and value ranges.

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:

  1. Math.random() → [0.0, 1.0)
  2. Multiply by n → [0.0, n)
  3. 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:

MethodReturn 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 +1 when the range is inclusive
  • Trying to store Math.pow() in an int without casting
  • Calling methods like x.abs() instead of Math.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.

Key Takeaways

Math methods are static and called with Math.methodName().
pow, sqrt, and random always return double.
Casting from double to int truncates the decimal part.
Math.random() returns values in [0.0, 1.0), never including 1.0.
The inclusive integer range formula is (int)(Math.random() * (max - min + 1)) + min.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse this website.

Notes

1 credit used · 5/5 remaining