From the free course AP Computer Science A on Lykke
1.11 Math Class
Key concepts: Java Math class · Absolute value with Math.abs · Integer arithmetic using Math.abs(int) · Decimal arithmetic using Math.abs(double) · Exponentiation using Math.pow · Square roots using Math.sqrt
A robot measuring a package may need the distance from a target, the area of a panel, or the square root of a sensor reading; Java’s Math class supplies these operations as ready-to-use static methods.
1.11 Math Class
A robot measuring a package may need the distance from a target, the area of a panel, or the square root of a sensor reading; Java’s Math class supplies these operations as ready-to-use static methods. The methods return numeric values, so their results can be stored, printed, compared, or combined with other expressions.
CED traceability — Topic 1.11: Learning Objective VAR-2; Essential Knowledge VAR-2.A.
Science practices: 1.A Design Code, 2.A Develop Code, and 3.A Analyze Code are applied when selecting a mathematical operation, writing the method call correctly, and tracing its returned value.
The shape of a Math method call
The Math class is used through its static methods: methods associated with the class itself rather than with a particular object. A call begins with the class name, followed by a dot and the method name:
double result = Math.sqrt(81.0);
The expression Math.sqrt(81.0) evaluates to 9.0, and that returned value is assigned to result. The method signature tells you both the required parameter type and the return type:
| Method | Meaning | Return type |
|---|---|---|
Math.abs(int x) |
Absolute value of an int |
int |
Math.abs(double x) |
Absolute value of a double |
double |
Math.pow(double base, double exponent) |
base raised to exponent |
double |
Math.sqrt(double x) |
Nonnegative square root of x |
double |
Absolute value: distance from zero
Absolute value removes a number’s sign. It can be understood as distance from zero: both $-7$ and $7$ are $7$ units from zero.
int temperatureChange = -7;
int magnitude = Math.abs(temperatureChange); // 7
double error = -2.75;
double errorSize = Math.abs(error); // 2.75
Math.abs(int x) returns the absolute value of an int. Because its return type is int, the result remains integer-valued. Math.abs(double x) returns the absolute value of a double, preserving decimal precision.
Worked example: checking a delivery error
A delivery system records a package’s actual position and its expected position. The sign indicates direction, but the monitoring system only needs the size of the error.
int actual = 42;
int expected = 50;
int difference = actual - expected; // -8
int errorMagnitude = Math.abs(difference); // 8
if (errorMagnitude <= 10) {
System.out.println("Within tolerance");
}
The subtraction produces $42 - 50 = -8$. Applying Math.abs changes that signed difference into its magnitude, $8$, so the condition is true.
Misconception check:
Math.absdoes not make every result adouble. The argument determines which overloaded method is selected: anintargument usesMath.abs(int), while adoubleargument usesMath.abs(double).
Exponentiation with Math.pow
Math.pow(double base, double exponent) returns the first parameter raised to the power of the second:
$$ \texttt{Math.pow(base, exponent)} = base^{exponent} $$
For example:
double area = Math.pow(5.0, 2.0); // 25.0
double growth = Math.pow(1.05, 3.0); // 1.157625...
Even when both arguments represent whole numbers, Math.pow returns a double. Thus, Math.pow(3, 2) produces the numeric value $9.0$, not an int value. It is not Java’s multiplication operator: 3 * 2 means $6$, whereas Math.pow(3, 2) means $3^2 = 9$.
Square roots with Math.sqrt
Math.sqrt(double x) returns the nonnegative square root of a double. The nonnegative qualification matters because both $4$ and $-4$ square to $16$, but Math.sqrt(16.0) returns 4.0.
double width = 6.0;
double height = 8.0;
double diagonal = Math.sqrt(Math.pow(width, 2.0)
+ Math.pow(height, 2.0));
// 10.0
This computes the diagonal of a rectangle using the Pythagorean relationship:
$$ d = \sqrt{w^2 + h^2} $$
For $w = 6$ and $h = 8$, the calculation is $\sqrt{36 + 64} = \sqrt{100} = 10$.
Misconception check:
Math.sqrtdoes not return both possible square roots. It returns only the nonnegative result, and its return type is alwaysdouble.
Retrieval check
What are the return types and values of these expressions?
Math.abs(-12) // ?
Math.abs(-12.5) // ?
Math.pow(2, 3) // ?
Math.sqrt(49.0) // ?
The answers are 12 as an int, 12.5 as a double, 8.0 as a double, and 7.0 as a double. The fastest reliable strategy is to read the method signature first, then evaluate the operation.

Source Materials
- AP Computer Science A Course and Exam Description
- AP Computer Science A Course and Exam Description Clarifications and Corrections
- AP Computer Science A Course Overview
- AP Computer Science A Course at a Glance
- AP Computer Science A Course at a Glance Poster
- Adopt AP Computer Science A
- AP Computer Science A
- AP Computer Science A