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:
To import the package “java.lang” with the “Math” class, we will write something like this:
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:
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:
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”:
Output
Example 3
Here, we will find out the square root of “100” using the “Math.sqrt()” method:
Output
Method 2: Import Java Math Class Using Import Statement
You can also utilize the “import” statement to import the Math class as follows:
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:
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:
Then, calculate the maximum number between “40” and “87” with the help of the “max()” method of Math class:
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:
Then, we will print out its predefined value using the “System.out.println()” method:
Output
Example 3
In this example, we will calculate the power of “10” using the “pow” method of the Math class:
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.