Java

# Java Math Class Methods | Explained with examples

Java math methods are used to perform several mathematical operations and the Java math class supports these methods. The Java Math class contains methods that perform several mathematical calculations like finding the logarithm, finding the square root, sorting the minimum and maximum values, solving trigonometric functions, and many more. This descriptive post has the following learning outcomes:

• Getting aware of various Java Math class methods
• Using Java Math class methods

## How Java math method works

As discussed above, the Java math class supports various methods that are associated with multiple operations. This section provides a list of several Java math class methods and states the purpose of each method.

Math.abs()
It is used to get the absolute(positive) value of the passed argument. The syntax of using this method is provided here:

Math.abs(a);

Math.max()
This Java class method accepts arguments and returns the largest number among them and the syntax to use this method is stated below:

Math.max(a,b);

Math.max()
It is practiced getting the minimum number between two values(that are passed as an argument). You can use this method by following the syntax provided below:

Math.min(a,b);

Math.round()
This method rounds off the decimal value to the nearest whole value. The method follows the syntax mentioned below:

Math.round(a);

Math.sqrt()
This method is exercised to get the square root of a value. It is used by following the syntax mentioned here:

Math.sqrt(a);

Math.cbrt()
It is practiced getting the cube root of a value by following the syntax stated below:

Math.cbrt(a);

Math.pow()
This method accepts two arguments and returns the first value with the power of the second argument. You may use the following syntax to practice this method:

Math.pow(a,b);

Math.log()
In Java math class, this method is used to get the natural algorithm of a value. The following syntax is used to practice this method.

Math.log(a);

Math.log10()
This method also refers to the algorithm and returns the algorithm to the base 10 of the value. To use this method, you must have to exercise the following syntax:

Math.log10(a);

Math.sin()
This belongs to a trigonometric category of math class methods and is used to apply the sin function on the passed argument.

Math.sin(a);

Math.cos()
This is also a trigonometric method and returns the cos of the value by using the syntax provided below.

Math.cos(a);

Math.tan()
This trigonometric math class method is used to calculate the tan function on the passed argument. The following syntax may be used for this method:

Math.tan(a);

Math.sinh()
The Sin hyperbolic function is used to find the hyperbolic value and it acts the same in the Java math class method. The syntax provided below is used to practice this method in Java:

Math.sinh(a);

Math.cosh()
This java math class method finds the hyperbolic Cosine value of the argument passed to it. The following syntax is followed to use this method:

Math.cosh(a);

Math.tanh()
The tan hyperbolic value of the argument can be found using this method. You may follow the syntax provided below to exercise this method:

Math.tanh(a);

Math.toDegrees()
This method converts the angle to an equivalent degree value. The argument passed to the method must be in radians and the syntax written below is followed to practice this method:

Math.toDegrees(a)

This method converts the degree measure of angle to radians. Here the argument value must be a degree passed to the method using the syntax provided below:

Note: The radians and degrees are the two measuring units of angles in mathematics.

## How to use Java math class methods

This section provides few examples that demonstrate the functionality of various java math class methods in Java code.

## Example 1: Using basic methods of Java math class

The following Java code provides the implementation of basic methods of Java math class.

package newpack;

public class MathMethods {
public static void main(String[]args) {
//initializing four integers
int a=4, b=6, c=-5, d=8;

//using Math.max(a,b) method
System.out.println(Math.max(a, b));

//using Math.min(a,b) method
System.out.println(Math.min(a, b));

//using Math.pow(a,b) method
System.out.println(Math.pow(a, b));

//using Math.sqrt(a) method
System.out.println(Math.sqrt(a));

//using Math.abs(c) method
System.out.println(Math.abs(c));

//using Math.cbrt(d) method
System.out.println(Math.cbrt(d));

}
}

The output of the above code is provided below: ## Example 2: Using trigonometric methods of Java math class

Several trigonometric java math class methods are practiced in the following Java code.

package newpack;

public class MathMethods {
public static void main(String[]args) {
//initializing an integer
int a=60;

//using Math.sin(a) method
System.out.println(Math.sin(a));

//using Math.cos(a) method
System.out.println(Math.cos(a));

//using Math.tan(a) method
System.out.println(Math.tan(a));

}
}

The code stated above is described below: ## Example 3: Using logarithm methods of Java math class

The Java code provided below makes use of various logarithmic methods of the Java math class.

package newpack;

public class MathMethods {
public static void main(String[]args) {
//initializing an integer
int a=100;

//using Math.sin(a) method
System.out.println(Math.log(a));

//using Math.log10(a) method
System.out.println(Math.log10(a));

}
}

The output of the code is shown in the image below; ## Conclusion

The math class of Java supports a long list of mathematical methods that assist in performing various mathematical calculations. This article provides the working of all methods of Java math class assisted by several examples as well. The methods of Java math class are categorized into basic, trigonometric, and logarithm methods. The basic methods perform basic operations of mathematics whereas the trigonometric methods are practiced performing various math functions like sin, cos, tan. Lastly, the logarithmic category includes the methods that allow finding the logarithm of a value in Java. 