Skip to content

Find absolute value of a number using abs method of Math Class

Java Math abs method example shows how to find the absolute value of an int, float, double or long number using the abs method of Java Math class.

How to find the absolute value of a number using the abs method in Java?

We can use the abs method of the Math class to get the absolute value of a number.

The abs method is overloaded for float, double, long, and int types. That means you can use the same method to get absolute value of float, double, long, and int numbers.

Output

You might be wondering why the absolute value of Integer.MIN_VALUE is -2147483648 instead of +2147483648. That is because an int is represented in Java by 32 bits and the highest value it can hold is +2147483647. Trying to represent +2147483648 number, it overflows and rolls over to -2147483648. This is the same case with the float, double and long types as well.

This example is a part of the Java Math class tutorial with examples.

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.