User Brian
Brian
Level 41

Java Math abs() method

Published in the Java Developer group

What is the absolute of a number?

The absolute value of a number is the equal to the number itself ignoring the sign. For example, the absolute of +5 is 5. Whereas, the absolute of -5 is also 5. Java Math abs() method - 1

What is Math.abs() method() in Java?

The java.lang.Math class provides a static method Math.abs(parameter) to find the “absolute value” of the parameter.
So, if you pass any positive number let’s say Math.abs(5) it will return 5. For a negative 5, Math.abs(-5) the result would be the same, i-e; 5.

Method Header


public static dataType abs(dataType parameter)

Allowed DataTypes

The abs() method of Java is overloaded for various data types. The allowed types are as under.
int float double long

Example 1


public class DriverClass {
    public static void main(String args[]) {
   
        int number = +5;
        // Print the original number
        System.out.println("Original Number = " + number);
 
        // Printing the absolute value
        // Calling the Math.abs() method
        System.out.println("Absolute Number = " + "Math.abs( " + number + " ) = " + Math.abs(number));
        
        
        number = -5;
        // Print the original number
        System.out.println("Original Number = " + number);
 
        // Printing the absolute value
        // Calling the Math.abs() method
        System.out.println("Absolute Number = " + "Math.abs( " + number + " ) = " + Math.abs(number));
        
    }
}

Output

Original Number = 5 Absolute Number = Math.abs( 5 ) = 5 Original Number = -5 Absolute Number = Math.abs( -5 ) = 5
As you can see from the output, the absolute of +5 and -5 is both equal to 5.

Example 2


public class DriverClass {
    public static void main(String args[]) {
   
        int number = -0;
        System.out.println("Original Number = " + number);
        System.out.println("Math.abs( " + number + " ) = " + Math.abs(number) + "\n");
        
        long number1 = -4499990;
        System.out.println("Original Number = " + number1);
        System.out.println("Math.abs( " + number1 + " ) = " + Math.abs(number1) + "\n");
        
        float number2 = -92.45f;
        System.out.println("Original Number = " + number2);
        System.out.println("Math.abs( " + number2 + " ) = " + Math.abs(number2) + "\n");
        
        double number3 = -63.7777777777;
        System.out.println("Original Number = " + number3);
        System.out.println("Math.abs( " + number3 + " ) = " + Math.abs(number3) + "\n");
    }
}

Output

Original Number = 0 Math.abs( 0 ) = 0 Original Number = -4499990 Math.abs( -4499990 ) = 4499990 Original Number = -92.45 Math.abs( -92.45 ) = 92.45 Original Number = -63.7777777777 Math.abs( -63.7777777777 ) = 63.7777777777

Boundary Cases

Here are some exceptional cases that you need to take care of while using Math.abs() method.

For int and long data types

If the argument is positive zero or negative zero, the result is positive zero.
Math.abs(+0) = 0 Math.abs(-0) = 0
For Integer.MIN_VALUE or Long.MIN_VALUE the output of Math.abs() is still the smallest integer or long which is negative.
Math.abs(Integer.MIN_VALUE) = -2147483648 Math.abs(Long.MIN_VALUE) = -9223372036854775808

For float and double data types

If the argument is infinite, the result is positive infinity.
Math.abs(Double.NEGATIVE_INFINITY) = Infinity
If the argument is NaN, the result is NaN.
Math.abs(Double.NaN) = NaN

Conclusion

By the end of this post, you must be familiar with the Java Math.abs() method. You can come across multiple day-to-day applications of this method. As always, we encourage you to learn by practicing. Till then, keep learning and keep growing! Java Math abs() method - 2
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION