Java Basic Examples

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

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

How to find absolute value of a number in Java?

Use abs() method of Math class to get absolute value of a number.

abs() method is overloaded for float, double, long and int. 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 absolute value of Integer.MIN_VALUE is -2147483648 instead of +2147483648. That is because 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. That is the same case with float, double and long as well.

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

Want to learn quickly?
Try one of the many quizzes. I promise you will not be disappointed.

Tags