TExcelTAD
Home › Java › Java Math

Java Math

The Math class from java.lang (imported automatically) bundles the everyday mathematical functions: minimum and maximum, powers and roots, rounding, absolute values, trigonometry and more. All of its methods are static, so you call them on the class itself: Math.max(a, b).

Minimum, maximum and absolute value

java · try it yourself
class Main {
    public static void main(String[] args) {
        System.out.println(Math.max(14, 9));
        System.out.println(Math.min(14, 9));
        System.out.println(Math.min(2.5, -1.0));
        System.out.println(Math.abs(-42));
        System.out.println(Math.abs(3.75));
    }
}
14
9
-1.0
42
3.75

The result type matches the arguments: two ints give an int, any double makes the result a double.

Powers and roots

java · try it yourself
class Main {
    public static void main(String[] args) {
        System.out.println(Math.pow(2, 10));    // 2 to the power of 10
        System.out.println(Math.sqrt(144));     // square root
        System.out.println(Math.cbrt(27));      // cube root
        System.out.println(Math.hypot(3, 4));   // sqrt(3*3 + 4*4)
        System.out.println(Math.PI);
        System.out.println(Math.E);
    }
}
1024.0
12.0
3.0
5.0
3.141592653589793
2.718281828459045

Math.pow and Math.sqrt always return a double. Cast to int or long if you need a whole number: (int) Math.pow(2, 10).

Rounding

Method Behaviour 2.5 -2.5 2.4
Math.round(x) nearest whole number, halves go up 3 -2 2
Math.floor(x) round down 2.0 -3.0 2.0
Math.ceil(x) round up 3.0 -2.0 3.0
Math.rint(x) nearest, halves go to the even number 2.0 -2.0 2.0
java · try it yourself
class Main {
    public static void main(String[] args) {
        double price = 7.4567;
        System.out.println(Math.round(price));
        System.out.println(Math.floor(price));
        System.out.println(Math.ceil(price));
        // Round to 2 decimal places
        System.out.println(Math.round(price * 100) / 100.0);
        // ...or just format it for display
        System.out.printf("%.2f%n", price);
    }
}
7
7.0
8.0
7.46
7.46

Integer helpers

Math also has methods that make whole-number arithmetic safer:

java · try it yourself
class Main {
    public static void main(String[] args) {
        System.out.println(Math.floorDiv(-7, 2));   // rounds toward negative infinity
        System.out.println(-7 / 2);                 // normal division truncates toward 0
        System.out.println(Math.floorMod(-7, 3));   // always non-negative for positive divisor
        System.out.println(Math.clamp(150, 0, 100)); // keep a value inside a range
        try {
            Math.addExact(Integer.MAX_VALUE, 1);
        } catch (ArithmeticException e) {
            System.out.println("Overflow detected: " + e.getMessage());
        }
    }
}
-4
-3
2
100
Overflow detected: integer overflow

Math.clamp arrived in Java 21. The ...Exact methods throw an exception instead of silently wrapping around.

Random numbers

Math.random() returns a double from 0.0 (inclusive) to 1.0 (exclusive). A common pattern for a whole number between 1 and 6:

java
int dice = (int) (Math.random() * 6) + 1;   // 1, 2, 3, 4, 5 or 6

Because the result is different every time, it isn't run here. For more control use the java.util.Random class or ThreadLocalRandom.current().nextInt(1, 7).

Quick reference

Method Description
Math.max(a, b) / Math.min(a, b) larger / smaller value
Math.abs(x) absolute value
Math.pow(a, b) a to the power b
Math.sqrt(x) / Math.cbrt(x) square / cube root
Math.round / floor / ceil rounding
Math.sin, cos, tan trigonometry (angles in radians)
Math.toRadians(deg) degrees to radians
Math.log(x) / Math.log10(x) natural / base-10 logarithm
Math.random() random double in [0, 1)

Test yourself

What does Math.sqrt(81) return?

  1. 9
  2. 9.0
  3. 81.0
  4. 3

What is Math.ceil(4.1)?

  1. 4.0
  2. 4
  3. 5.0
  4. 4.1

Which expression gives a random whole number from 0 to 9?

  1. Math.random(10)
  2. (int) (Math.random() * 10)
  3. Math.random() * 10
  4. (int) Math.random() * 10