Math Class

Naveen AnamaneniUpdated · 4 min read

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

MethodDoesExampleResult
Math.abs(x)Absolute valueMath.abs(-7)7
Math.max(a, b) / minLarger / smaller of twoMath.max(3, 9)9
Math.pow(a, b)a to the power b (a double)Math.pow(2, 10)1024.0
Math.sqrt(x) / cbrtSquare / cube rootMath.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) / ceilRound down / up (a double)Math.ceil(2.1)3.0
Math.PI, Math.EConstantsMath.PI3.141592653589793

A Practical Example

Delivery.java▶ Try it
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);
    }
}
Output
Distance: 5.0 km
Boxes needed: 3
Fare: Rs. 63
Area within 1.5 km: 7.07 sq km

How it works

  1. Line 5: hypot(3, 4) gives the straight-line distance, 5.0.
  2. Line 6: ceil rounds up, because 2.3 kg needs 3 one-kg boxes.
  3. Line 7: round(62.5) gives 63: halves round up.
  4. Line 8: the area of a circle, π × r², with pow for the square.

Try it yourself: change the weight to 2.0. How many boxes now?

Rounding Surprises

Rounding.java▶ Try it
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));
    }
}
Output
3 -2 2
-3.0 -2.0
-3 -1
-4 1
  1. Math.round rounds halves towards positive infinity: 2.5 → 3, but -2.5 → -2.
  2. floor always goes down (-2.5 → -3.0); ceil always goes up (-2.5 → -2.0).
  3. / on integers cuts towards zero (-3), and % keeps the sign of the left side (-1).
  4. floorDiv and floorMod round 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

  1. Easy: Print the hypotenuse of a right triangle with sides 5 and 12.
  2. 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).

Quick check: Math class

4 questions
1What does Math.round(-2.5) return?
2What type does Math.pow(2, 3) return?
3What does Math.ceil(2.1) return?
4Do you need an import to use Math?

Key Takeaways

Was this tutorial helpful?

Lesson 89 of 482