codegurukool.com · Free coding tutorials & online compiler

Home › Java Tutorial › Java math

Java math

The Math class.

The Math class has ready-made helpers: max, min, sqrt, pow, abs, round, ceil and floor. Math.random() gives a decimal from 0 up to (not including) 1.

Example code

public class Main {
  public static void main(String[] args) {
    System.out.println(Math.max(3, 9));
    System.out.println(Math.min(3, 9));
    System.out.println(Math.sqrt(16));
    System.out.println(Math.pow(2, 10));
    System.out.println(Math.abs(-5));
    System.out.println(Math.round(2.6));
    System.out.println(Math.ceil(2.1));
  }
}

Run this code in the online editor

← Java stringsJava booleans →