Java Math

Java Math is a built-in class that provides useful methods and constants for common numeric calculations. You use it when you need operations such as absolute values, comparisons, powers, square roots, and rounding.

The Math class is part of java.lang, so you do not need an import statement to use it.

Calling Math Methods

Most Math features are called with the class name, a dot, and the method name. For example, Math.abs(-5) returns 5.

public class Main {
    public static void main(String[] args) {
        int temperature = -12;
        int limit = 7;

        System.out.println("Absolute: " + Math.abs(temperature));
        System.out.println("Higher: " + Math.max(temperature, limit));
        System.out.println("Lower: " + Math.min(temperature, limit));
    }
}

Output:

Absolute: 12
Higher: 7
Lower: -12

Math.abs() removes the negative sign from a number. Math.max() returns the larger of two values, and Math.min() returns the smaller one.

Powers, Square Roots, And PI

The Math class also includes methods for calculations that are common in geometry and science. Use Math.pow(base, exponent) for powers and Math.sqrt(value) for square roots.

Math.PI is a constant that stores the value of pi as a double.

public class Main {
    public static void main(String[] args) {
        double radius = 4.0;
        double area = Math.PI * Math.pow(radius, 2);
        double roundedArea = Math.round(area * 100.0) / 100.0;

        System.out.println("Area: " + roundedArea);
        System.out.println("Square root: " + Math.sqrt(81));
    }
}

Output:

Area: 50.27
Square root: 9.0

Because many Math methods work with double values, their results can include decimal places. In the example, the area is rounded to two decimal places by multiplying by 100.0, rounding, and dividing by 100.0.

Rounding Numbers

Java provides several rounding methods, and they do slightly different jobs.

Method What it does
Math.floor(x) Rounds down to the nearest whole number as a double.
Math.ceil(x) Rounds up to the nearest whole number as a double.
Math.round(x) Rounds to the nearest whole number.
public class Main {
    public static void main(String[] args) {
        double price = 19.75;

        System.out.println("Floor: " + Math.floor(price));
        System.out.println("Ceiling: " + Math.ceil(price));
        System.out.println("Rounded: " + Math.round(price));
    }
}

Output:

Floor: 19.0
Ceiling: 20.0
Rounded: 20

Use floor() when you always need to move down, ceil() when you always need to move up, and round() when you want the nearest whole number.

Common Math Methods

  • Math.abs(x) returns the absolute value.
  • Math.max(a, b) returns the larger value.
  • Math.min(a, b) returns the smaller value.
  • Math.pow(a, b) raises a to the power of b.
  • Math.sqrt(x) returns the square root.
  • Math.floor(x), Math.ceil(x), and Math.round(x) handle rounding.

A Note About Random Numbers

Math.random() returns a random double from 0.0 up to, but not including, 1.0. Because the result changes each time, avoid relying on one exact value from Math.random().

Takeaway: Java’s Math class gives you ready-made methods for common numeric work, so you can write clearer calculations with less code.