Import Math Class in Java

The Math class in Java is available in java.lang package. Since java.lang package is the default package to every Java program, therefore no need to import Math class explicitly in the program. But all of its variables and methods are static.

There are two ways to access static variables or methods of Math class,

Example directly using class name,

public class Test {
   public static void main(String[] args) {

      System.out.println(Math.PI); // 3.141592653589793
      System.out.println(Math.E); // 2.718281828459045

      System.out.println(Math.pow(2, 3)); // 8.0
      System.out.println(Math.exp(1)); // 2.718281828459045
      System.out.println(Math.log10(10)); // 1.0

      System.out.println(Math.sqrt(64)); // 8.0
      System.out.println(Math.cbrt(64)); // 4.0

      System.out.println(Math.abs(-20)); // 20
      System.out.println(Math.max(10, -20)); // 10
      System.out.println(Math.min(10, -20)); // -20
   }
}

Output:-

3.141592653589793
2.718281828459045
8.0
2.718281828459045
1.0
8.0
4.0
20
10
-20

Program:- Java program to find area of a circle.

public class CircleArea{
   public static void main(String[] args) {
      int radius = 10;

      // circle area = π * r * r
      double area = Math.PI * Math.pow(radius, 2);

      // display area
      System.out.println(area);
   }
}

Output:-

314.1592653589793

Static import of Math class in Java

If we import Math class statically and then we can invoke static members without calling through it’s class name.

Statement to import Math class all static members:- import static java.lang.Math.*;

The “import static java.lang.Math.*;” statement will import all static variables and methods of the Math class.

Example of importing math in java.

import static java.lang.Math.*;
public class Test {
   public static void main(String[] args) {

      System.out.println(PI); // 3.141592653589793
      System.out.println(E); // 2.718281828459045

      System.out.println(pow(2, 3)); // 8.0
      System.out.println(exp(1)); // 2.718281828459045
      System.out.println(log10(10)); // 1.0

      System.out.println(sqrt(64)); // 8.0
      System.out.println(cbrt(64)); // 4.0

      System.out.println(abs(-20)); // 20
      System.out.println(max(10, -20)); // 10
      System.out.println(min(10, -20)); // -20
   }
}

If we want to import only a particular static member using the static import concept then we have to use the following syntax:- import static package.className.staticMember;

It will import only a particular static member. Example:- import static Math.PI; to import only PI variable, import static Math.sqrt; to import only sqrt() method.

Program:- Java program to find the area of a circle through a static importing of math class in Java,

import static java.lang.Math.PI;
import static java.lang.Math.pow;
public class Test{
   public static void main(String[] args) {
      int radius = 10;

      // circle area = π * r * r
      double area = PI * pow(radius, 2);

      // display area
      System.out.println(area);
   }
}

Output:-

314.1592653589793

Note that Math class contains several methods having multiple overloaded forms. For example abs(), max() and min() methods contains 4 overloaded forms per method.

The overloaded forms of Math.max() are,

  • public static int max(int a, int b)
  • public static long max(long a, long b)
  • public static float max(float a, float b)
  • public static double max(double a, double b)

If the method contains several overloaded forms and we are using static import to import only that particular method then all of its overloaded forms are also imported.

import static java.lang.Math.max;
public class Test{
   public static void main(String[] args) {

      // int max(int a, int b)
      System.out.println(max(10, 20)); // 20

      // long max(long a, long b)
      System.out.println(max(40L, 30L)); // 40

      // float max(float a, float b)
      System.out.println(max(10.5F, 19.9F)); // 19.9

      // double max(double a, double b)
      System.out.println(max(25.0, 15.5)); // 25.0
   }
}

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Also See,

Leave a Reply