Shiklo Programming
EN
  • English
  • Español
  • Português
  • Deutsch
  • Français
  • 日本語
  • हिन्दी

Java Math

Java's Math class is a toolbox of ready-made mathematical functions. You do not create anything: you call its methods directly as Math.name(...). These methods work with the number types from Data Types and are used everywhere, from games to finance.

Largest, smallest and absolute value

public class Main {
    public static void main(String[] args) {
        System.out.println(Math.max(12, 30));
        System.out.println(Math.min(12, 30));
        System.out.println(Math.abs(-7.5));
        System.out.println(Math.max(Math.max(4, 9), 6));
    }
}
Output
30
12
7.5
9

Math.max takes two values, so to find the largest of three you can nest the calls, as in the last line.

Powers and roots

Math.pow(a, b) raises a to the power b, and Math.sqrt gives the square root. Both return a double:

public class Main {
    public static void main(String[] args) {
        System.out.println(Math.pow(2, 10));
        System.out.println(Math.sqrt(144));
        System.out.println(Math.cbrt(27));
        double side = 3, other = 4;
        double hypotenuse = Math.sqrt(side * side + other * other);
        System.out.println(hypotenuse);
    }
}
Output
1024.0
12.0
3.0
5.0

The last lines use Pythagoras' theorem to find the long side of a right triangle.

Rounding

There are several ways to turn a decimal number into a whole one. Math.round rounds to the nearest whole number, Math.floor always goes down and Math.ceil always goes up:

public class Main {
    public static void main(String[] args) {
        double x = 4.5;
        System.out.println(Math.round(x));
        System.out.println(Math.floor(x));
        System.out.println(Math.ceil(x));
        System.out.println(Math.round(-4.5));
        double price = 19.4567;
        System.out.println(Math.round(price * 100) / 100.0);
    }
}
Output
5
4.0
5.0
-4
19.46

The last line is a common trick to round to two decimals: multiply by 100, round, then divide by 100.0. Note that round(-4.5) gives -4, because halves are rounded up, towards positive infinity. Compare this with casting, which simply cuts off the decimals, in Type Casting.

Constants and random numbers

Math.PI and Math.E hold the famous constants. Math.random() returns a random double that is at least 0.0 and less than 1.0. Multiply and cast it to get a whole number in a range:

public class Main {
    public static void main(String[] args) {
        double radius = 2;
        System.out.printf("Area: %.2f%n", Math.PI * radius * radius);
        int dice = (int) (Math.random() * 6) + 1;
        System.out.println(dice >= 1 && dice <= 6);
    }
}
Output
Area: 12.57
true

Because the dice value changes on every run, the example prints whether it is in the right range instead of the value itself. The Random Numbers topic shows the Random class, which has easier methods for this.

Practice exercises

  1. Print the larger and the smaller of 45 and 72.
  2. Compute the area of a circle with radius 7, rounded to one decimal.
  3. Print a random whole number from 1 to 100.