# Java Math Class Tutorial with Examples

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.

Output

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`.

Output

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).

Output

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.

## 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.

Output

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.

Output

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.

Output

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.

Output

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.

Output

## 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.

Output

## 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.

Output

## 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.

Output

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.

Output

## 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.

Output

## 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.

Output

## 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,

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.

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

Output

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.

Output

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.

Output

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

Output

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).

Output

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

References:

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

#### RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.