Introduction
Java Math class provides several methods to work on math calculations like min(), max(), avg(), sin(), cos(), tan(), round(), ceil(), floor(), abs() etc. In this article, we will learn about the Java Math class, its basic methods, and constructors provided by the Java programming language.
Math class in Java
The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions. Unlike some of the numeric methods of class StrictMath, all implementations of the equivalent functions of class Math are not defined to return the bit-for-bit the same results. This relaxation permits better-performing implementations where strict reproducibility is not required.
By default, many of the Math methods simply call the equivalent method in StrictMath for their implementation. Code generators are encouraged to use platform-specific native libraries or microprocessor instructions, where available, to provide higher-performance implementations of Math methods. Such higher-performance implementations still must conform to the specification for Math.
If the size is int or long and the results overflow the range of value, the methods addExact(), subtractExact(), multiplyExact(), and toIntExact() throw an ArithmeticException.
For other arithmetic operations like increment, decrement, divide, absolute value, and negation, overflow occurs only with a specific minimum or maximum value. It should be checked against the maximum and minimum values as appropriate.
Math class declaration
Following is the declaration for java.lang.Math class.
public final class Math
extends Object
The complete example program of java.lang.Math class is listed below.
public class MathClassExample1 {
public static void main(String[] args) {
double x = 28;
double y = 4;
System.out.println("Maximum number of x and y is: " + Math.max(x, y));
System.out.println("Square root of y is: " + Math.sqrt(y));
System.out.println("Power of x and y is: " + Math.pow(x, y));
System.out.println("Logarithm of x is: " + Math.log(x));
System.out.println("Logarithm of y is: " + Math.log(y));
System.out.println("return the logrithn of log10 of x is: " + Math.log10(x));
}
}
The above program generates the following output.
What is the NaN value in Math class?
NaN stands for not a number. Nan is produced if a floating-point operation has some input parameters that cause the operation to produce some undefined result. For example, 0.0 divided by 0.0 is arithmetically undefined. Finding out the square root of a negative number, too, is undefined.
The complete program of NaN value.
public class NaNValueExample {
public static void main(String[] args) {
System.out.println(2.0 % 0);
System.out.println(0.0 / 0);
System.out.println(Math.sqrt(-1));
}
}
The above program generates the following output.
How to compare NaN values?
All numeric operations with NaN as an operand produce NaN as a result. The reason behind this is that NaN is unordered, so a numeric comparison operation involving one or two NaNs returns false.
- The numerical comparison operators <, <=, >, and >= always return false if either or both operands are NaN.(§15.20.1)
- The equality operator == returns false if either operand is NaN.
- The inequality operator != returns true if either operand is NaN. (§15.21.1).
The complete program of comparing the NaN values.
public class CompareNaNValues {
public static void main(String[] args) {
System.out.print("Check if equal :");
System.out.println(Float.NaN == Float.NaN);
System.out.print("Check if UNequal: ");
System.out.println(Float.NaN != Float.NaN);
System.out.print("Check if equal: ");
System.out.println(Double.NaN == Double.NaN);
System.out.print("Check if UNequal: ");
System.out.println(Double.NaN != Double.NaN);
double NaN = 2.1 % 0;
System.out.println((2.1 % 0) == NaN);
System.out.println(NaN == NaN);
}
}
The above program generates the following output.
Math class methods in Java
The java.lang.Math class contains various methods for performing basic numeric operations such as the logarithm, cube root, and trigonometric functions, etc. The various Java math methods are as follows:
1) Math.abs() method in Java
The java.lang.Math.abs() method returns the absolute (positive) value of an int value. This method gives the absolute value of the argument. The argument can be int, double, long, and float.
- If the argument is positive zero or negative zero, the result is positive zero.
- If the argument is infinite, the result is positive infinity.
- If the argument is NaN, the result is NaN.
Syntax
public static int abs(int i)
public static double abs(double d)
public static float abs(float f)
public static long abs(long lng)
The complete program of java.lang.Math.abs() method example is listed below.
import java.lang.Math;
public class MathClassExample2 {
public static void main(String[] args) {
double x = 4876.1874d;
double y = -0.0d;
// get and print their absolute values
System.out.println("absolute value of x =" + Math.abs(x));
System.out.println("absolute value of y =" + Math.abs(y));
System.out.println("absolute value of (-9999.555d)=" + Math.abs(-9999.555d));
}
}
The above program generates the following output.
2) Math.acos() method in Java
The java.lang.Math.acos(double a) returns the arc cosine of an angle in the range of 0.0 through pi. If the argument is NaN or its absolute value is greater than 1, then the result is NaN. The results must be semi-monotonic.
Syntax
public static double acos(double a)
The complete program of java.lang.Math.acos(double a) method example is listed below.
import java.lang.Math;
public class MathClassExample3 {
public static void main(String[] args) {
double x = Math.PI / 2;
x = Math.toRadians(x);
System.out.println("Math.acos() example" + Math.acos(x));
}
}
The above program generates the following output.
3) Math.asin() method in Java
The java.lang.Math.asin(double a) returns the arc sine value of the method argument passed. The returned angle is in the range -pi/2 to pi/2.
- If the argument is NaN or its absolute value is greater than 1, then the result is NaN.
- If the argument is zero, then the result is a zero with the same sign as the argument.
The results must be semi-monotonic.
Syntax
public static double asin(double a)
The complete program of java.lang.Math.asin(double a)) method example is listed below.
public class MathClassExample4 {
public static void main(String[] args) {
double x = Math.PI / 2;
System.out.println("Math.asin() example =" + Math.asin(x));
}
}
The above program generates the following output.
4) Math.toRadians() method in Java
The java.lang.Math.toRadians(double deg) converts an angle measured in degrees to an approximately equivalent angle measured in radians. Math class usually takes radians as an input which is very much different in real-life applications since angles are usually represented in degrees.
Syntax
public static double toRadians(double deg)
The complete program of java.lang.Math.toRadians(double deg) method example is listed below.
import java.lang.Math;
public class MathClassExample5 {
public static void main(String[] args) {
double x = 45;
double y = -180;
x = Math.toRadians(x);
y = Math.toRadians(y);
System.out.println("Math.toRadius() of x = " + Math.toRadians(x));
System.out.println("Math.toRadius() of y = " + Math.toRadians(y));
}
}
The above program generates the following output.
5) Math.max() method in Java
The java.lang.math.max() is an inbuilt method in Java that is used to return the Maximum or Largest value from the given two arguments. The arguments are taken in int, float, double and long.
- If we provide positive and negative values as an argument, this method will return a positive argument.
- If we provide both negative values as an argument, the number with the lower magnitude is returned as a result.
- If the arguments are not a number(NaN), this method will return NaN.
Syntax
public static int max(int a, int b)
public static double max(double a, double b)
public static long max(long a, long b)
public static float max(float a, float b)
The complete program of java.lang.math.max()) method example is listed below.
public class MathClassExample6 {
public static void main(String args[]) {
//print the maximum of two numbers
//Compare two integer numbers
int x = 20;
int y = 50;
System.out.println(Math.max(x, y));
//Compare two double number
double a = 25.67;
double b = -38.67;
System.out.println(Math.max(a, b));
//Compare two float numbers
float p = -25.74f;
float q = -20.38f;
System.out.println(Math.max(p, q));
}
}
The above program generates the following output.

6) Math.min() method in Java
The java.lang.math.min() is an inbuilt method in Java that is used to return the Minimum or Lowest value from the given two arguments. The arguments are taken in int, float, double and long
- If we provide positive and negative values as an argument, this method will return a negative argument.
- If we provide both negative values as an argument, the number with a higher magnitude is returned as a result.
- If the arguments are not a number(NaN), this method will return NaN.
Syntax
public static int min(int a, int b)
public static double min(double a, double b)
public static long min(long a, long b)
public static float min(float a, float b)
The complete program of java.lang.math.min() method example is listed below.
public class MathClassExample7 {
public static void main(String args[])
{
//print the minimum of two numbers
int x = 20;
int y = 50;
//print the minimun value of integer values
System.out.println(Math.min(x, y));
double a = 25.67;
double b = -38.67;
//print the minimum value of double values
System.out.println(Math.min(a, b));
float p = -55.73f;
float q = -30.95f;
//print the minimum value of float values
System.out.println(Math.min(p, q));
}
} 
Join the conversation! Your thoughts help the community grow.