Java

How to Import Math in Java?

Java language is the collection of packages, classes, and objects. More specifically, the Java Math class is available in java.lang package, the default package of Java. The Math class in Java is utilized for different purposes. It contains multiple methods to perform calculations, such as finding square roots and logs.

This tutorial will demonstrate how to import math in Java.

How to Import Math in Java?

As we discussed above, the Math class belongs to java.lang package, so it helps to access the Math class implicitly. Now we will look at how we can import the Java Math class.

Syntax
The syntax to import any package or class is given as:

import packagename.classname;

To import the package “java.lang” with the “Math” class, we will write something like this:

import java.lang.Math

However, the methods and variables of the Math class are static, so you can access them by using two ways:

  • Import Java Math class without using import statement
  • Import Java Math class by using import statement

We will now check each of the mentioned methods one by one!

Method 1: Import Java Math Class Without Using Import Statement

In Java, you can use the “Math” class directly without importing it because the Math class belongs to the “java.lang” package that allows using the associated classes implicitly. In this way, we can access the methods of the Math class with the class name. For example:

Math.PI

Have a look at the given examples for calling the static methods and variables of the Math class without importing them.

Example 1
In this example, we will access the “PI” static variable of the “Math” class to get the value of Pi. The most common example of using Pi is finding the area of a circle in any language. In the Java programming language, you can call the PI variable without importing “java.lang.Math package” as follows:

System.out.println("The value of PI:" + Math.PI);

Output

Example 2
In the below example, we will call the “max()” method of the Math class to find the maximum number between “40” and “87”:

System.out.println("The maxiumum number:" + Math.max(40,87));

Output

Example 3
Here, we will find out the square root of “100” using the “Math.sqrt()” method:

System.out.println("The square root of 100 is " + Math.sqrt(100));

Output

Method 2: Import Java Math Class Using Import Statement

You can also utilize the “import” statement to import the Math class as follows:

import static java.lang.Math.*;

Asterisk (*) means “all”, and it signifies that all of the static variables and methods of the Math class will be imported.

The other way to use the import statement is add the variable or method name at the end like this:

import static java.lang.Math.PI;

The above-given statement will only import the “PI” static variable from the Math class.

Note: This method is more useful as it only requires a one-time definition at the top of the program. After that, you can utilize any method or variable without importing the Math class again and again.

Example 1
Here, we will first import the “java.lang.Math.*” class. Next, we will calculate the square root of a number “100” using the “sqrt()” method:

System.out.println("The square root of 100 is " + sqrt(100));

Then, calculate the maximum number between “40” and “87” with the help of the “max()” method of Math class:

System.out.println("The maxiumun number:" + max(40,87));

Note that we are accessing both methods without mentioning the Math class, as it is imported before:

Output

Example 2
Now, we will import the PI variable with the Java Math class:

import static java.lang.Math.PI;

Then, we will print out its predefined value using the “System.out.println()” method:

System.out.println("The value of PI:" + PI);

Output

 

Example 3
In this example, we will calculate the power of “10” using the “pow” method of the Math class:

System.out.println("The square of 10: " + pow(10,2));

Output

We have gathered all the basic and important information related to importing Math in Java.

Conclusion

To import Math in Java, you can use the “java.lang” package to access the methods or variables of the Java Math class with their class name. Another way to import a Math class is to add the “java.lang.Math.*” import statement at the top of the code. It will allow access to all the members and the variables of the Math class without mentioning the class name. In this tutorial, we discussed different ways to import Math in Java.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.