JAVA Math Class

JAVA Math Class

JAVA Math class is a class that provides more advanced maths calculations than Java Math. It has direct methods for programs such as rounding off numbers, logarithmic algorithms, the factorial of a number, and concatenation of string, trigonometric functions, finding max and min of two numbers in the math package. Some of the useful methods are round(), fact(), min(), max(), avg(), sin(), cos(), tan(), ceil(), floor() etc.

All these methods are found in java.lang package and the math class not necessarily need to be imported. But we can use a static import in case of extensively using these functions. As a result, the code throws an Arithmetic exception when the range overflows.

For example, we take two integers and apply addExact() to add their value and the output doesn’t lie in the range of the value. It will throw an Arithmetic exception. For other such operations such as increment, subtraction, decrement, negation etc, the overflow will occur with a specific range of either minimum or maximum. The value needs to be checked properly before applying the math class method.

Here is how we declare the java.lang.Math class in Java:

public final class Maths
extends object

Math methods used in Java


Math.abs()
It gives the absolute value for the given value.
Math.max()It will return the maximum value of two integers.
Math.min()It will return the minimum value of two integers.
Math.round()It will round the decimal value to the nearest value.
Math.sqrt()It will give the square root value of a number.
Math.ceil()It is used to find the smallest number which is greater than or equal to the argument.
Math.floor()It is used to find the largest number which is lesser than or equal to the argument.
Math.addExact()It will give the sum of two integers and will throw and exception in case the value increases.
Math.subtractExact()It will give the difference of two integers and will throw and exception in case the value underflow the range.
Math.cbrt()It will give the cube root value of a number.
Math.log()It will return the natural log value of the input.
Math.sin()This will return the trigonometric sin value of the input double integer.
Math.cos()This will return the trigonometric cos value of the input double integer.
Math.hypot()This will return the hypotenuse of a right triangle while we pass base and perpendicular as the inputs.
Math.toDegrees()This will convert the radians to equivalent angles in degree value.
Math.toRadians()This will convert the degree value to equivalent angles in radians value.

Below is a program which states the uses of some math functions:

import java.math.*;
public class HelloWorld{

     public static void main(String []args)
     {
     int ValueI = -10; 
        float ValueF = .10f; 
     
        System.out.println("Initial value of integer  : "+ValueI); 
        System.out.println("Initial value of integer F  : "+ValueF); 

        int AbsoluteI = Math.abs(ValueI); 
        float Absolutef = Math.abs(ValueF); 
  
        System.out.println("Absolute value of integer : "+AbsoluteI); 
        System.out.println("Absolute value of integer F : "+Absolutef); 
        System.out.println(""); 
  
        double Acosi = Math.acos(30); 
        System.out.println("acos value of Acosi : "+Acosi); 
        double a = Math.PI; 
        a = Math.toRadians(a); 
        double Acosj = Math.acos(a); 
        System.out.println("acos value of Acosj : "+Acosj);
    } 
}

The output of this program will be:

Initial value of integer  : -10
Initial value of integer F  : 0.1
Absolute value of integer : 10
Absolute value of integer F : 0.1

acos value of Acosi : NaN
acos value of Acosj : 1.5159376794536454

Leave a comment

Your email address will not be published.