01 / 20
M ath f

The Java
Math Class

Master the static methods of java.lang.Math: absolute value, exponents, square roots, and random numbers for the AP exam.

AP Computer Science A

What Is the Math Class?

Java provides a built-in class called Math in the java.lang package. It contains static methods for common mathematical operations: you never create a Math object: you call each method directly on the class itself.

Why the Math Class Matters

AP Exam
Math.abs, Math.pow, Math.sqrt, and Math.random appear directly on the AP CSA exam.
No Import Needed
java.lang.Math is auto-imported, so you can use it anywhere without an import statement.
Static Only
You cannot instantiate Math. All methods are called as Math.method(), reinforcing how static works.

The Four Key Methods

Math.abs(x)
Returns \(\lvert x \rvert\)
int or double Math.pow(b, e)
Returns \(b^e\)
returns double Math.sqrt(x)
Returns \(\sqrt{x}\)
returns double Math.random()
Returns \([0.0, 1.0)\)
returns double

Math.abs: Absolute Value

Math.abs(x) returns the absolute value of \( x \). It is overloaded: passing an int returns an int; passing a double returns a double.

int a = Math.abs(-7); // 7 (int) double b = Math.abs(-3.14); // 3.14 (double) int c = Math.abs(5); // 5 (int)

Math.pow: Exponentiation

Math.pow(base, exponent) returns \( \text{base}^{\text{exponent}} \). Both parameters are doubles and it always returns a double: even \( 2^3 \) returns 8.0, not 8.

double x = Math.pow(2, 3); // 8.0 double y = Math.pow(3, 0.5); // 1.7320... (sqrt 3) int z = (int) Math.pow(2, 3); // 8 (cast to int)

Math.sqrt: Square Root

Math.sqrt(x) returns \( \sqrt{x} \) as a double. Passing a negative value returns NaN (Not a Number).

double a = Math.sqrt(25); // 5.0 double b = Math.sqrt(2); // 1.41421356... double c = Math.sqrt(-1); // NaN

Math.random: Random Numbers

Math.random() returns a double \( d \) such that \( 0.0 \leq d < 1.0 \). To generate integers in a range, you scale and cast.

// Random double in [0.0, 1.0) double r = Math.random(); // Random int from 0 to 9 int d = (int)(Math.random() * 10); // Random int from 1 to 6 (dice roll) int die = (int)(Math.random() * 6) + 1;

int vs double Return Types

Method Parameter Return Type
Math.abs(int) int int
Math.abs(double) double double
Math.pow(double, double) double, double double
Math.sqrt(double) double double
Math.random() none double

How to Apply the Math Class

1
Identify the operation. Do you need absolute value, a power, a square root, or a random number?
2
Choose the correct method. Use Math.abs, Math.pow, Math.sqrt, or Math.random() accordingly.
3
Check the return type. If you need an int from Math.pow or Math.random, cast explicitly with (int).
4
Store or use the result. Assign to a variable of the correct type: avoid storing a double in an int without casting.

Example 1: Distance Between Points

Find the distance between \( (1, 2) \) and \( (4, 6) \) using the distance formula: \( d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2} \).

int x1 = 1, y1 = 2, x2 = 4, y2 = 6; double dist = Math.sqrt( Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2) ); // sqrt(9 + 16) = sqrt(25) = 5.0

Example 2: Random Range

Generate a random integer between 10 and 50 (inclusive).

// Formula: (int)(Math.random() * (max - min + 1)) + min int min = 10; int max = 50; int rand = (int)(Math.random() * (max - min + 1)) + min; // rand is in [10, 50]

A tabletop dice experiment linked to its full outcome space

A red die and an ivory die rest on a teal felt tray.A die roll can be generated with (int)(Math.random() * 6) + 1. Two independently generated rolls form an ordered pair of values from 1 through 6.

Two dice, 36 ordered outcomes

A 6-by-6 outcome grid marks the six ordered pairs whose values sum to seven.For two independent fair dice, six of the 36 equally likely ordered pairs sum to seven, so P(sum = 7) = 1/6. The red die supplies the row and the ivory die the column.

Example 3: Combining Methods

What is the output of the following code?

int a = Math.abs(-4); // 4 double b = Math.pow(a, 2); // 16.0 double c = Math.sqrt(b + 9); // sqrt(25.0) = 5.0 int result = (int) c; // 5 System.out.println(result); // prints 5

Common Mistakes

✕ Forgetting the Cast
Storing Math.pow or Math.random result in an int without (int) causes a compile error.
✕ Off-by-One in Random
Using * (max - min) instead of * (max - min + 1) excludes the upper bound.
✕ Creating a Math Object
Writing new Math() does not compile. All methods are static: use Math.method().

Summary Reference Table

Call Returns Example
Math.abs(-7) 7 int
Math.abs(-3.5) 3.5 double
Math.pow(2, 10) 1024.0 double
Math.sqrt(144) 12.0 double
Math.random() \([0.0, 1.0)\) double
(int)(Math.random()*6)+1 \([1, 6]\) int

Question 1 of 5

What is the value of Math.abs(-12)?

Question 2 of 5

What does Math.pow(3, 2) return?

Question 3 of 5

Which expression generates a random int from 5 to 15 (inclusive)?

Question 4 of 5

What is the output of the following code?

Question 5 of 5

Which statement about the Math class is false?