Math Class
You don't build your own calculator to find a square root; you press the √ key. Java has its own set of keys: the Math class. It's in java.lang, so there's nothing to import, and all its methods are static, called as Math.sqrt(144). This lesson covers the ones you'll use most, plus a few results that surprise people in exams and interviews.
The Everyday Methods
| Method | Does | Example | Result |
|---|---|---|---|
Math.abs(x) | Absolute value | Math.abs(-7) | 7 |
Math.max(a, b) / min | Larger / smaller of two | Math.max(3, 9) | 9 |
Math.pow(a, b) | a to the power b (a double) | Math.pow(2, 10) | 1024.0 |
Math.sqrt(x) / cbrt | Square / cube root | Math.sqrt(144) | 12.0 |
Math.hypot(x, y) | √(x² + y²) | Math.hypot(3, 4) | 5.0 |
Math.round(x) | Nearest whole number (a long for double) | Math.round(2.5) | 3 |
Math.floor(x) / ceil | Round down / up (a double) | Math.ceil(2.1) | 3.0 |
Math.PI, Math.E | Constants | Math.PI | 3.141592653589793 |
A Practical Example
public class Delivery {
public static void main(String[] args) {
double dx = 3.0, dy = 4.0;
double weightKg = 2.3;
double distance = Math.hypot(dx, dy);
double boxes = Math.ceil(weightKg / 1.0);
long fare = Math.round(distance * 12.5);
double area = Math.PI * Math.pow(1.5, 2);
System.out.println("Distance: " + distance + " km");
System.out.println("Boxes needed: " + (int) boxes);
System.out.println("Fare: Rs. " + fare);
System.out.printf("Area within 1.5 km: %.2f sq km%n", area);
}
}Distance: 5.0 km
Boxes needed: 3
Fare: Rs. 63
Area within 1.5 km: 7.07 sq kmHow it works
- Line 5:
hypot(3, 4)gives the straight-line distance, 5.0. - Line 6:
ceilrounds up, because 2.3 kg needs 3 one-kg boxes. - Line 7:
round(62.5)gives 63: halves round up. - Line 8: the area of a circle, π × r², with
powfor the square.
Try it yourself: change the weight to 2.0. How many boxes now?
Rounding Surprises
public class Rounding {
public static void main(String[] args) {
System.out.println(Math.round(2.5) + " " + Math.round(-2.5)
+ " " + Math.round(2.4));
System.out.println(Math.floor(-2.5) + " " + Math.ceil(-2.5));
System.out.println(-7 / 2 + " " + -7 % 2);
System.out.println(Math.floorDiv(-7, 2) + " " + Math.floorMod(-7, 2));
}
}3 -2 2
-3.0 -2.0
-3 -1
-4 1Math.roundrounds halves towards positive infinity: 2.5 → 3, but -2.5 → -2.flooralways goes down (-2.5 → -3.0);ceilalways goes up (-2.5 → -2.0)./on integers cuts towards zero (-3), and%keeps the sign of the left side (-1).floorDivandfloorModround down instead, which is what you want for things like "which week is day -7 in?" or wrapping around a clock.
Exact and Safe Arithmetic
Math.addExact, subtractExact, multiplyExact and toIntExact do normal arithmetic but throw ArithmeticException on overflow instead of silently wrapping (see "Integer Overflow and Floating-Point Precision"). Use them for money and counters that must never wrap.
Common Mistakes
Expecting pow or sqrt to return an int. They return double; cast or round when you need a whole number.
Using Math.round for money in paise. It works on double, which is inexact; prefer BigDecimal with an explicit rounding mode for financial values.
Writing Math.max(a, b, c). max takes exactly two arguments: Math.max(a, Math.max(b, c)).
Practice
- Easy: Print the hypotenuse of a right triangle with sides 5 and 12.
- Harder: Work out how many full pages are needed to print 1,234 lines at 50 lines per page, using
Math.ceil(or integer arithmetic only).
Key Takeaways
Was this tutorial helpful?