How to import Math class in Java with examples

In this post, I will be sharing how to import Math class in Java with examples. The Math class is present in the default package i.e. java.lang package. As a result, we do not need to explicitly import the Math class into the Java program.

Read Also: Dice Roll Source Code in Java

Let's dive deep into the topic:

How to import Math class in Java with examples

The syntax to import Math class in Java is:

 import java.lang.Math;


According to Oracle docs, all the methods and variables are static. We can access the static variables or static methods of Math class by using two ways.

1. Using Class Name

2. Using static import statement

1. Using Class Name


We can easily access the Math class static variables and methods by using class name i.e. Math.pow(), Math.sqrt(), etc. as shown below in the example:

 public class MathClassExample {

  public static void main(String args[]) {

    // Accessing static variables of Math class by using class name
    // Below line will print PI value
    System.out.println(Math.PI);// Output: 3.141592653589793

    // Below line will print E value
    System.out.println(Math.E);// Output: 2.718281828459045
    
    // Accessing static methods of Math class by using class name
    // Below line will calculate 6^2
    System.out.println(Math.pow(6,2));// Output: 36.0
    
    // Below line will calculate (log(base10)100)
    System.out.println(Math.log10(100));// Output: 2.0
    
    // Below line will calculate cube root
    System.out.println(Math.cbrt(125));// Output: 5.0
    
    // Below line will calculate square root
    System.out.println(Math.sqrt(49));// Output: 7.0
    
    // Below line will print max value
    System.out.println(Math.max(34, 89));// Output: 89
    
    // Below line will print min value
    System.out.println(Math.min(134, -83));// Output: -83
    
    // Below line will calculate absolute value
    System.out.println(Math.abs(-456));// Output: 456
    
    // Below line will return random value between 0.0 and 1.0
    System.out.println(Math.random());
  }
}



2. Using static import statement


We can also use the static import statement to invoke the static members without calling their class name.

  ðŸ’¡ Did You Know?

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


 import static java.lang.Math.*;

public class MathClassExample2 {
    
  public static void main(String args[]) {
    
    // Accessing static variables of Math class by using static import statement
    // Below line will display PI value
    System.out.println(PI);// Output: 3.141592653589793
    
    // Below line will display E value
    System.out.println(E);// Output: 2.718281828459045
    
    // Accessing static methods of Math class by using static import statement
    // Below line will print 8^3
    System.out.println(pow(8,3));// Output: 512.0
    
    // Below line will print (log(base10)1000)
    System.out.println(log10(1000));// Output: 3.0
    
    // Below line will print cube root
    System.out.println(cbrt(729));// Output: 9.0
    
    // Below line will print square root
    System.out.println(sqrt(81));// Output: 9.0
    
    // Below line will display max value
    System.out.println(max(534, 189));// Output: 534
    
    // Below line will display min value
    System.out.println(min(325, 983));// Output: 325
    
    // Below line will print absolute value
    System.out.println(abs(-14));// Output: 14 
    
    // Below line will print and return random value between 0.0 and 1.0
    System.out.println(random());
  }
}


Import particular static member using static import


The syntax for importing a particular static member of the Math class is:

 import packageName.className.staticMember;

The above line will import a particular static member. For example, we are importing PI static variable using import static java.lang.Math.PI; statement as shown below:

 import static java.lang.Math.PI;

public class MathClassExample3 {
    
  public static void main(String args[]) {
    
    // Accessing static variables of Math class by using static import statement
    // Below line will show PI value
    System.out.println(PI * 2);// Output: 6.283185307179586
    
  }
}

That's all for today. Please mention in the comments if you have any questions related to how to import Math class in Java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry