1.11

Math Class

The Math class provides static methods for absolute value, power, square root, and generating random numbers.

1525% of exam
Understand It
Ace It
Context

What this topic is and why it exists

Imagine you have a Swiss Army knife — one compact tool that handles dozens of jobs.
Java's `Math` class is exactly that for numbers.
It's already built into every Java program you'll ever write (no imports needed), and it hands you powerful operations with simple one-line calls.
Need to strip away a negative sign? `Math.abs(-7)` gives you `7`.
Want to raise 2 to the 10th power? `Math.pow(2, 10)` returns `1024.0`.
Curious about the square root of 144? `Math.144\sqrt{144}` delivers `12.0`.
Notice these methods all start with `Math.` followed by the method name — that's because they belong to the class itself, not to any object you create.
You never write `new Math()`.
You just call the method directly, pass in your values, and get a result back.
The trickiest gem in the toolkit is `Math.random()`, which returns a decimal from 0.0 up to (but never quite reaching) 1.0.
Want a random integer from 1 to 6, like rolling a die?
Multiply by 6, cast to an `int`, and add 1: `(int)(Math.random() * 6) + 1`.
Learn to stretch and shift that 0-to-1 range, and you can generate any random number you need.
Master these few methods now, and you'll reach for them constantly throughout the AP exam.
1 / 9