How to import Math class in Java?

In Java, you can use Math class directly, without importing it.

Math class is part of the java.lang package, that is implicitly imported everywhere:

public class Example {

  public static void main(String[] arg) {
    double squareOfTwo = Math.pow(2, 2);

    System.out.println(squareOfTwo); // 4.0
  }
}

Static imports

You can also use static imports to shorten your code:

import static java.lang.Math.pow;

public class Example {

  public static void main(String[] arg) {
    double squareOfTwo = pow(2, 2);

    System.out.println(squareOfTwo); // 4.0
  }
}