Guide to Importing the Math Class in Java | ninjasquad

Java is a programming language with a collection of packages, classes, and objects. The Java Math class is available in java.lang package, which is the default package of Java. The Java Math class has multiple methods that can be used to perform calculations, such as finding the square, square roots, tan(), cos(), and logs. This article will demonstrate the various ways you can use to import math in Java.

The Java Math class performs more complex mathematical computations than the fundamental Java math operators. To import Math in Java, you can use the “java.lang” package to access the methods or variables of the Java Math class using their class name. Another way of importing a Math class is to use the “java.lang.Math.*” import statement at the top of the code. This will allow access to all the members and the variables of the Math class without mentioning the class name.

## Importing the Math Class in Java

The syntax used to import any package, as well as the class, is given as follows:

import packagename.classname;

To import the package “java.lang” with the “Math” class, we will have to write the code shown below:

The methods and variables of the Math class are static and hence can be accessed using the two ways highlighted below:

- How to import Java Math class without using the import statement
- How to import Java Math class while using the import statement

Let us now discuss each of the methods in detail.

### Method 1: How to import Java Math class without using the import statement

In Java, the Math class belongs to the “java.lang” package; hence you can use the “Math” class without importing it. This package allows using the associated classes implicitly. In such a way, the methods of the Math class can be accessed with the class name. Let us look at the example below that demonstrates how to import the Java Math class without using the import statement:

In the example above, we can access the “PI,” which is a static variable of the “Math” class, to get the pi value. The most common example of using Pi is finding the area of a circle. In Java programming language, one can call the PI variable without having to import the “java.lang.Math package,” as shown below:

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

We shall execute the following examples to get a more concrete understanding of the syntax shown above: Feel free to try them since they have been tried and tested by our experts.

##### Example 1:

package JavaProgram; public class example { public static void main (String[] args) { System.out.println("The value of PI:" + Math.PI); } }

The output of the code is:

The value of PI:3.141592653589793

##### Example 2:

In this example, we will call the “max()” method of the Math class so that we can find the maximum number between “10” and “50”.

package JavaProgram; public class example { public static void main (String[] args) { System.out.println("The maximum number is : " + Math.max(10,50)); } }

The output of the code is:

The maximum number is : 50

##### Example 3:

In this example, we will find the square root of “729” by using the “Math.sqrt()” method

package JavaProgram; public class example { public static void main (String[] args) { System.out.println("The square root of 729 is : " + Math.sqrt(729)); } }

The output of the code is:

The square root of 729 is : 27.0

### Method 2: How to import Java Math class while using the import statement

You can utilize the “import” statement to import the Math class using the syntax shown below:

import static java.lang.Math.*;

The asterisk (*) refers to “all.” It also signifies that all the static variables and methods of the class will be imported. Another way of using the import statement is to add the variable or method name at the end, as shown below:

import static java.lang.Math.PI;

The statement above will only import the “PI” static variable from the Math class. This method is more useful since it only requires a one-time definition at the top of the program. Once complete, you can utilize any way and variable without having to import the Math class again.

Check out the examples shown below to get a clear understanding of this subject matter:

##### Example 1:

We will first import the “java.lang.Math.*” class in this example. After that, we will calculate the square root of the number “729” by using the “sqrt()” method.

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

Once this is complete, we will try to find the value of Pi using the “PI” method.

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

Lastly, we will calculate the maximum number between “10” and “50” with the help of the “max()” method of Math class.

System.out.println("The maximum number is :" + max(10,50));

Remember that we are mentioning the methods without saying the Math class since it has already been imported. Below is the complete code showing all the calculations entailed in this example”

package JavaProgram; import static java.lang.Math.*; public class example { public static void main (String[] args) { System.out.println("The square root of 729 is :" + sqrt(729)); System.out.println("The value of PI:" + PI); System.out.println("The maximum number is :" + max(10,50)); } }

The output of the code is:

The square root of 729 is :27.0 The value of PI:3.141592653589793 The maximum number is :50

##### Example 2:

In this example, we will calculate the power of “10” using the “pow” method of the Math class. First, we shall use the syntax below:

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

Below is the complete code to be used:

package JavaProgram; import static java.lang.Math.*; public class example { public static void main (String[] args) { System.out.println("The square of 17 is :" + pow(17, 2)); } }

The output of the code is:

The square of 17 is : 289.0

## Conclusion

Methods for finding the maximum or minimum of two numbers, rounding values, logarithmic functions, square root, and trigonometric functions (sin, cos, tan, etc.) are all included in the Math class. All the different Math class functions were discussed in this article. Feel free to try any of the choices provided. Thanks for reading.

Source: Internet