# Java Math Class

## We’ll cover the following

• Math class
• Math class constants
• Java mathematical functions
• Examples

## Math class

Java’s built-in operators are useful, but they cannot provide all the mathematical needs of most Java programmers.

We can use Java Math class to do more complicated math equation.

Java Math class includes methods that perform a wide variety of mathematical calculations, from basic functions such as calculating an absolute value or a square root to trigonometry functions such as sin and cos (sine and cosine), to practical functions such as rounding numbers or generating random numbers.

All the methods of the Math class are declared as static methods, which means you can use them by specifying the class name Math followed by a period and a method name.

Here’s a statement that calculates the square root of a number stored in a variable named y:

``````double x = Math.sqrt(y);
``````

The Math class is contained in the java.lang package, which is automatically available to all Java programs. As a result, you don’t have to provide an import statement to use the Math class.

## Math class constants

The Math class defines two constants that are useful for many mathematical calculations.

Note that these constants are only approximate values, because both PI and e are irrational numbers.

The following program illustrates a typical use of the constant PI.

The program calculates the area of the circle.

``````package JAVA.Basic;

public class Area {
public static void main(String[] args) {
double r, area;
r = 10.8; // radius of circle
area = Math.PI * r * r; // compute area
System.out.println("Area of circle is " + area);
}
}
``````

The Math class’s PI constant has a precision of just 15 digits.

## Java mathematical functions

The following table lists the basic mathematical functions that are provided by the Math class.

The following code uses the Mathematical Methods of the Math Class

``````package JAVA.Basic;

public class MathsClass {
public static void main(String[] args) {
int a = 10;
int b = -5;
int c = 3;
double x = 25.0;
double y = 3.0;
double z = 4.0;

System.out.println("abs(b) = " + Math.abs(b));
System.out.println("cbrt(x) = " + Math.cbrt(x));
System.out.println("exp(y) = " + Math.exp(z));
System.out.println("hypot(y,z)= " + Math.hypot(y, z));
System.out.println("log(y) = " + Math.log(y));
System.out.println("log10(y) = " + Math.log10(y));
System.out.println("max(a, b) = " + Math.max(a, b));
System.out.println("min(a, b) = " + Math.min(a, b));
System.out.println("pow(a, c) = " + Math.pow(a, c));
System.out.println("random() = " + Math.random());
System.out.println("signum(b) = " + Math.signum(b));
System.out.println("sqrt(x) = " + Math.sqrt(y));

}
}
``````

Output of the following code is :

## More Examples

You can use the abs and signnum methods to force the sign of one variable to match the sign of another, like this:

``````int a = 27;
int b = -32;

a = Math.abs(a) * Math.signum(b); // a is now -27;
``````

You can use the pow method to square a number, like this:

``````double x = 4.0;

double y = Math.pow(x, 2); // a is now 16;
``````

Simply multiplying the number by itself, however, is often just as easy and just as readable:

``````double x = 4.0;

double y = x * x; // a is now 16;
``````

That’s it!
You have successfully completed the post. Do Share : )

Peace Out!

Also Read – Java String and String Literals

Check Out Deals on -> Amazon , Flipkart , Myntra , Adidas , Apple TV , Boat , Canva , Beardo , Coursera , Cleartrip , Fiverr , MamaEarth , Swiggy