# Java Math Class

Math class is a utility class to calculations on numeric type values. Math class has several methods which help us to provide many calculations related concept like addition, subtraction, min, max, etc.

Math class is present in java.lang package. It provides constants and static methods for trigonometric, log arithmetic and other common mathematical functions.

Internal Architecture of Math class:

1. This is a final class, hence we cannot extend this class.

2. Its default constructor is private, hence we cannot create an instance of this class.

3. All methods present inside this class is static, hence no need to create an object. Method calls can be easily made using the class name.

4. Math class has two constants PI and E.

These are the following static methods present inside Math class-

 Sr. No Method Description 1. abs() Returns the absolute value. 2. cbrt() Returns the cube root of a value. 3. ceil() Returns the smallest integer greater than given decimal value. 4. max(double, double) Returns the greater of two double values. 5. min(double, double) Returns the smaller of two double values. 6. sqrt(double) Returns the square root of a double value. 7. random() Returns a random positive double value between 0 to 1. 8. exp() Returns Euler’s number e raised to the power of value.

## Java Math Class Example

``````public class Demo
{
public static void main(String[] args)
{
double a = 10;
double b = 20;

// return the maximum of two numbers
System.out.println("Maximum number of a and b is: " +Math.max(a, b));

// return the square root of y
System.out.println("Square root of b is: " + Math.sqrt(b));

//returns 28 power of 4 i.e. 28*28*28*28
System.out.println("Power of a and b is: " + Math.pow(a, b));

// return the logarithm of given value
System.out.println("Logarithm of a is: " + Math.log(a));
System.out.println("Logarithm of b is: " + Math.log(b));

// return the logarithm of given value when base is 10
System.out.println("log10 of a is: " + Math.log10(a));
System.out.println("log10 of b is: " + Math.log10(b));

// return the log of a + 1
System.out.println("log1p of a is: " +Math.log1p(a));

// return a power of 2
System.out.println("exp of a is: " +Math.exp(a));

// return (a power of 2)-1
System.out.println("expm1 of a is: " +Math.expm1(a));
}
}``````
Example Output