Java Math class tutorial with examples will help you understand how to use the Java Math class in an easy way. Math class in Java provides utility methods to do basic numeric operations like calculating square roots, exponential and logarithm. The Math class in Java is declared as a final class and all the utility methods are defined as static methods. In addition to that, the Math class also defines two fields, E and PI.

Below given are some of the very useful methods provided by the Math class in Java.

## How to find the absolute value of a number?

The `abs`

method of the Math class returns the absolute value of a given number. If the argument number is a non-negative number, the same number is returned. If the argument number is a negative number, the negation of the argument number is returned.

1 2 3 4 5 6 7 8 9 10 11 12 | int i = 5; int j = -5; int k = 0; //returns the same number, as it is positive System.out.println(i); //returns -(-5) = 5 System.out.println(j); //returns 0 System.out.println(k); |

Output

1 2 3 | 5 -5 0 |

The `abs`

method has been overloaded for int, long, float, and double types.

**Note**: For the float and double types, if the argument is infinite, positive infinity is returned, and if the argument is NaN value then the result is also NaN. In the case of int and long numbers, if the argument is either `Integer.MIN_VALUE`

or `Long.MIN_VALUE`

, the result is also the same value.

## How to find a natural logarithm (base e) of a number?

The `log`

method of the Math class returns the natural logarithm of a number. The natural logarithm is a logarithm using base e, also known as the Euler’s number i.e. `2.71828`

.

1 2 3 4 5 6 7 8 | double d = 20.2; /* * base e logarithm * will print a number around 3 * because 2.71828 x 2.71828 x 2.71828 = around 20.2 */ System.out.println( Math.log(d) ); |

Output

1 | 3.005682604407159 |

There are some special cases. For example, if the argument is NaN or less than 0, the result is NaN. If the argument is positive infinity, the result is also positive infinity. If the argument is positive or negative zero, the result is negative infinity.

## How to find base 10 logarithm of a number?

The `log10`

method returns base 10 logarithm of a number. Sometimes, the base 10 logarithm is written without mentioning the base like log(100).

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | double d1 = 100; double d2 = 1000; /* * logarithm base 10 * will print 2 * because 10 x 10 = 100 */ System.out.println( Math.log10(d1) ); /* * Similarly log(1000) is 3 * because 10 x 10 x 10 = 1000 */ System.out.println( Math.log10(d2) ); |

Output

1 2 | 2.0 3.0 |

Just like the `log`

method, the `log10`

method has some special cases too. If the argument is NaN or less than zero, the result is NaN. If the argument is positive infinity, the result is also positive infinity. If the argument is positive or negative zero, the result is negative infinity.

## How to find power?

The `pow`

method returns the result of the first argument raised to the power of the second argument. Both of the arguments are of type double.

1 2 3 4 5 6 7 8 9 | double d1 = 2; double d2 = 3; /* * To find the power use * pow method. */ //will print 2 x 2 x 2 = 8 System.out.println( Math.pow(d1, d2) ); |

1 | 8.0 |

## How to find the square root of a number?

To find the square root of a number, use the `sqrt`

method. The `sqrt`

method returns rounded positive square root of a given double value.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | double d1 = 4; double d2 = 9; /* * To find the square root of a number, use * sqrt method. */ //will print 2, as square root of 4 is 2 System.out.println( Math.sqrt(d1) ); //will print 3, as square root of 9 is 3 System.out.println( Math.sqrt(d2) ); //remember, the result is rounded! This will also return 3. System.out.println( Math.sqrt(9.0000000000000001) ); |

Output

1 2 3 | 2.0 3.0 3.0 |

If the argument value is NaN or less than zero, the result is NaN. If the argument is positive infinity, the result is also positive infinity. If the argument is positive or negative zero, the return value is also positive or negative zero respectively.

## How to find the cube root of a number?

To find the cube root of a number, use the `cbrt`

method. The `cbrt`

method returns the cube root of a given double value.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | double d1 = 8; double d2 = 27; /* * To find the cube root of a number, use * cbrt method. */ //will print 2, as cube root of 8 is 2 ( 2x2x2 = 8 ) System.out.println( Math.cbrt(d1) ); //will print 3, as cube root of 27 is 3 ( 3x3x3 = 27 ) System.out.println( Math.cbrt(d2) ); //remember, the result is rounded! This will also return 2. System.out.println( Math.cbrt(8.0000000000000001) ); |

Output

1 2 3 | 2.0 3.0 2.0 |

If the argument number is NaN, the result is also NaN. If the argument is infinite, the result is infinity with the same sign as that of the argument. If the argument is zero, the result is also zero with the same sign as the argument.

## How to find a maximum of two numbers?

The `max`

method returns the greater number of the given two argument numbers.

1 2 3 4 5 6 7 8 9 | double d1 = 13232.43; double d2 = 13123.43; /* * To find the maximum of two given numbers, * use the max method. */ //will print 13232.43 System.out.println( Math.max(d1, d2) ); |

Output

1 | 13232.43 |

The `max`

method is overloaded for int, long, float, and double types. For float and double types, if either argument is NaN, then the result is also NaN.

## How to find a minimum of two numbers?

The `min`

method returns the smaller number of the given two argument numbers.

1 2 3 4 5 6 7 8 9 | double d1 = 8233223.98; double d2 = 8233223.99; /* * To find the minimum of two given numbers, * use the min method. */ //will print 8233223.98 System.out.println( Math.min(d1, d2) ); |

Output

1 | 8233223.98 |

The `min`

method is overloaded for int, long, float, and double types. For double and float types, if either argument is NaN, then the result is also NaN.

## How to round a number?

The `round`

method of the Math class rounds a number to the closest integer or long value. The `round`

method is overloaded for types float and double and returns int and long respectively.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | /* * To round a number, * use the round method. * * It adds 0.5 to a number, takes the floor * value and then cast it to the integer value. */ //will print 2, because 2.01 + 0.5 = 2.51, floor(2.51) = 2.0, int(2.0) = 2 System.out.println( Math.round(2.01) ); //will print 2, because 2.49 + 0.5 = 2.99, floor(2.99) = 2.0, int(2.0) = 2 System.out.println( Math.round(2.49) ); //will print 3, because 2.50 + 0.5 = 3.0, floor(3.0) = 3.0, int(3.0) = 3 System.out.println( Math.round(2.50) ); //will print 3, because 2.99 + 0.5 = 3.49, floor(3.49) = 3.0, int(3.0) = 3 System.out.println( Math.round(2.99) ); //negative numbers //will print -2, because -2.01 + 0.5 = -1.51, floor(-1.51) = -2.0, int(-2.0) = -2 System.out.println( Math.round(-2.01) ); //will print -2, because -2.49 + 0.5 = -1.99, floor(-1.99) = -2.0, int(-2.0) = -2 System.out.println( Math.round(-2.01) ); //will print -2, because -2.50 + 0.5 = -2.00, floor(-2.0) = -2.0, int(-2.0) = -2 System.out.println( Math.round(-2.50) ); //will print -3, because -2.51 + 0.5 = -2.01, floor(-2.01) = -3.0, int(-3.0) = -3 System.out.println( Math.round(-2.51) ); |

Output

1 2 3 4 5 6 7 8 | 2 2 3 3 -2 -2 -2 -3 |

## How to round up a number?

The `ceil`

method is used to round up a number. The ceil is a short form of the ceiling to denote the upward direction. The `ceil`

method returns the smallest integer value which is greater than or equal to the given argument.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /* * To round up a number, use * the ceil method. */ //will print 2.0 System.out.println( Math.ceil(2.0) ); //will print 3.0 System.out.println( Math.ceil(2.01) ); //will print -2.0 System.out.println( Math.ceil(-2.0) ); //will print -2.0 System.out.println( Math.ceil(-2.01) ); //will print -2.0 System.out.println( Math.ceil(-2.99) ); |

Output

1 2 3 4 5 | 2.0 3.0 -2.0 -2.0 -2.0 |

## How to round down a number?

The `floor`

method of the Math class is used to round down a number. The `floor`

method returns the largest integer number that is less than or equal to the given argument value.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /* * To round down a number, use * the floor method. */ //will print 2 System.out.println( Math.floor(2.0) ); //will print 2 System.out.println( Math.floor(2.01) ); //will print 2 System.out.println( Math.floor(2.99) ); //will print -2 System.out.println( Math.floor(-2.0) ); //will print -3 System.out.println( Math.floor(-2.01) ); //will print -3 System.out.println( Math.floor(-2.99) ); |

Output

1 2 3 4 5 6 | 2.0 2.0 2.0 -2.0 -3.0 -3.0 |

## Differences between round, floor and ceil methods of the Math class

1. The return type of the `round`

method is either int or long for float and double type respectively. While the `ceil`

and `floor`

methods return a double value.

2. The `ceil`

method always returns an integer number which is greater than or equal to the given argument. Similarly, the `floor`

method always returns an integer number which is less than or equal to the given argument. In the case of the `round`

method, the returned number can be equal to, less than, or greater than the given argument number.

The following table shows different values returned from the `round`

, `floor`

and `ceil`

methods.

Number | round | floor | ceil |

1.0 | 1 | 1.0 | 1.0 |

1.01 | 1 | 1.0 | `2.0` |

1.50 | `2` | 1.0 | 2.0 |

1.99 | 2 | 1.0 | 2.0 |

0 | 0 | 0.0 | 0.0 |

-0 | 0 | 0.0 | 0.0 |

-1.0 | -1 | -1.0 | -1.0 |

-1.01 | -1 | `-2.0` | -1.0 |

-1.50 | -1 | -2.0 | -1.0 |

-1.51 | `-2` | -2.0 | -1.0 |

-1.99 | -2 | -2.0 | -1.0 |

## The copySign method

The `copySign`

method returns the first argument number with the sign of the second argument. The actual value of argument two does not matter, only the sign is copied.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //will print 2.02 (as the second argument is a positive value) System.out.println( Math.copySign(-2.02, 10.0) ); //will also print 2.02 (the second value does not matter, only sign is copied) System.out.println( Math.copySign(-2.02, 100000.000) ); //will print -2.02 (as the second argument is a negative value) System.out.println( Math.copySign(2.02, -3.23) ); //will print -2.02 System.out.println( Math.copySign(-2.02, -3.23) ); //will print -2.02 (as the second argument is a negative value) System.out.println( Math.copySign(2.02, -0.0) ); |

Output

1 2 3 4 5 | 2.02 2.02 -2.02 -2.02 -2.02 |

The `copySign`

method is overloaded to accept float and double types.

## The exp method

The `exp`

method of the Math class returns e raised to the power of the argument value. The value of the e (Euler’s number) is 2.7182818284590452 (and more digits). So the function `exp(2)`

returns 2.7182818284590452 x 2.7182818284590452.

1 | System.out.println( Math.exp(2) ); |

Output

1 | 7.38905609893065 |

## How to find sine, cosine, and tangent of an angle?

The sine, cosine, and tangent are the most widely used trigonometric functions based on an angle of a right-angled triangle. Java Math class provides `sin`

, `cos`

, and `tan`

functions for calculating sine, cosine, and tangent of an angle respectively.

The `sin`

, `cos`

, and `tan`

methods accept an angle as radians. If you have an angle in degrees, you first need to convert it to the radians using the `toRadians`

method.

1 2 3 4 5 6 7 8 | /* * Calculate sin, cos, and tan of 30 degree angle. * Remember to convert degrees to radians first! */ System.out.println( "Sin of 30 degree: " + Math.sin( Math.toRadians(30) ) ); System.out.println( "Cos of 30 degree: " + Math.cos( Math.toRadians(30) ) ); System.out.println( "Tan of 30 degree: " + Math.tan( Math.toRadians(30) ) ); |

Output

1 2 3 | Sin of 30 degree: 0.49999999999999994 Cos of 30 degree: 0.8660254037844387 Tan of 30 degree: 0.5773502691896257 |

## How to find hyperbolic sine, cosine, and tangent?

Java Math class provides `sinh`

, `cosh`

and `tanh`

functions for hyperbolic sine, cosine, and tangent respectively. The names are similar to the functions of trigonometric functions with letter ‘h’ added at the end to denote the hyperbolic.

The hyperbolic calculations are based on exponential function and are very straight forward.

cos x = (e^x + e^-x)/2

sin x = (e^x – e^-x)/2

tan x = sin/cos

You can also implement these methods without using built-in `sinh`

, `cosh`

, and `tanh`

methods by using the `exp`

method as given below.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /* * The cosh */ //using built-in method cosh System.out.println("Buil-in cosh method: " + Math.cosh(2) ); System.out.println("Using exp method: " + (Math.exp(2) + Math.exp(-2))/2d ); /* * The sinh */ //using built-in method sinh System.out.println("Buil-in sinh method: " + Math.sinh(2) ); System.out.println("Using exp method: " + (Math.exp(2) - Math.exp(-2))/2d ); /* * The tanh */ //using built-in method tanh System.out.println("Buil-in tanh method: " + Math.tanh(2) ); double cosh = (Math.exp(2) + Math.exp(-2))/2d; double sinh = (Math.exp(2) - Math.exp(-2))/2d; System.out.println("Using exp method: " + sinh/cosh ); |

Output

1 2 3 4 5 6 | Buil-in cosh method: 3.7621956910836314 Using exp method: 3.7621956910836314 Buil-in sinh method: 3.626860407847019 Using exp method: 3.626860407847019 Buil-in tanh method: 0.9640275800758169 Using exp method: 0.964027580075817 |

## The *exact methods

The Java version 8 has added some very useful methods having names ending with the “Exact” word for various arithmetic operations. These methods are,

**1. The addExact method:**

The `addExact`

method returns the sum of the two argument numbers. This method is overloaded for int and long types and throws ArithmeticException if the result overflows an int or long value respectively.

**2. The subtractExact method:**

The `subtractExact`

method returns subtraction of the two argument numbers. This method is overloaded for int and long types and throws ArithmeticException if the result overflows an int or long value respectively.

**3. The multiplyExact method:**

The `multiplyExact`

method returns multiplication of the two argument numbers. This method is overloaded for int and long types and throws ArithmeticException if the result overflows an int or long value respectively.

**4. The incrementExact method:**

The `incrementExact`

method returns the argument number incremented by one. This method is overloaded for int and long types and throws ArithmeticException if the result overflows an int or long value respectively.

**5. The decrementExact method:**

The `decrementExact`

method returns the argument number decremented by one. This method is overloaded for int and long types and throws ArithmeticException if the result overflows an int or long value respectively.

**6. The negateExact method:**

The `negateExact`

method negates the given argument number. This method is overloaded for int and long types and throws ArithmeticException if the result overflows an int or long value respectively.

**7. The toIntExact method:**

The `toIntExact`

method converts the given long value to the int value. This method throws ArithmeticException if the result overflows an int value.

Why do we need special methods for the regular arithmetic operations? Consider below given example to understand that.

1 2 3 4 | int i = 100; int j = 100000000; System.out.println( i * j ); |

You will expect the output to be 10000000000. Let’s have a look at the actual output.

Output

1 | 1410065408 |

The reason is, the result of the multiplication was out of the range of the int value. The Java arithmetic operators like +, -, *, etc. do not report the overflow. The programmer would not know if the multiplication operation was out of the range of the int value at the run time and hence will give the incorrect results. In the case of the arithmetic operators, It is the programmer’s responsibility to check every time that the result stays in the range of the given type.

Now let’s try the same program with the `multiplyExact`

method.

1 2 3 4 | int i = 100; int j = 100000000; System.out.println( Math.multiplyExact(i, j) ); |

Output

1 2 3 | Exception in thread "main" java.lang.ArithmeticException: integer overflow at java.lang.Math.multiplyExact(Unknown Source) at Example.main(Example.java:121) |

The *exact method throws ArithmeticException if the arithmetic operation result is out of the range of the int or long types and makes programmer’s life easier.

## The nextUp and nextDown methods

The `nextUp`

method returns a floating point value which is slightly greater than the argument value. Similarly, the `nextDown`

method returns a floating point number which is slightly less than the argument value. Both of these methods are overloaded for the float and double types.

1 2 3 4 | System.out.println( Math.nextUp(-1.01) ); System.out.println( Math.nextUp(1) ); System.out.println( Math.nextUp(1.01) ); System.out.println( Math.nextUp(1.001) ); |

Output

1 2 3 4 | -1.0099999999999998 1.0000001 1.0100000000000002 1.0010000000000001 |

As you can see from the output, the returned values are slightly greater than the argument value.

1 2 3 4 | System.out.println( Math.nextDown(-1.01) ); System.out.println( Math.nextDown(1) ); System.out.println( Math.nextDown(1.01) ); System.out.println( Math.nextDown(1.001) ); |

Output

1 2 3 4 | -1.0100000000000002 0.99999994 1.0099999999999998 1.0009999999999997 |

Similarly, for the `nextDown`

method, the return values are slightly smaller than the argument value. These two methods are particularly useful when you want a number that is slightly greater or less than the number you have.

## How to generate random numbers?

The `random`

method of the Math class generates and returns a random number of the double type which is greater than or equal to 0 and less than 1.0 (0.0 <= n < 1.0).

1 2 3 4 | System.out.println( Math.random() ); System.out.println( Math.random() ); System.out.println( Math.random() ); System.out.println( Math.random() ); |

Output

1 2 3 4 | 0.33036530832118016 0.11107575617437915 0.8153600571088704 0.6189547871899587 |

You may get a different output than me, as the numbers are generated randomly. Please visit the how to generate random numbers in a given range example to learn to generate random numbers between two numbers, say for example, between 10 and 20 or 0 and 100.

The below given Java Math examples will help you understand various methods of the Math class in more detail.

## Java Math Examples

- How to generate random number using random method of Java Math class
- How to calculate logarithm value using log method of Java Math class
- How to find exponential using pow method of Java Math class
- How to find minimum number using min method of Java Math class
- How to find maximum number using max method of Java Math class
- How to round a number using round method of Java Math class
- How to round down a number using floor method of Java Math class
- How to round up a number using ceil method of Java Math class
- How to find absolute value of a number using abs method of Java Math class

References:

Java Math class Javadoc

Please let me know if you liked the Java Math class tutorial with examples in the comments section below.

## Add Comment