Find Absolute Value in Java – Math.abs()

To find the absolute value of a number, the abs() method is given in the java.lang.Math class. There are several overloaded forms of abs() method to accept different types of arguments and return accurate value. All of them return the absolute value of an argument.

  • If the argument is not negative, then the same argument is returned.
  • If the argument is negative, then the negation of the argument is returned.

The overloaded forms of java.lang.Math.abs() method are,

  • public static int abs(int a)
  • public static long abs(long a)
  • public static float abs(float a)
  • public static double abs(double a)

These methods are implemented like,

public static int abs(int a) {
   return (a < 0) ? -a : a;
}

Math.abs() examples

public class Test{
   public static void main(String[] args) {

      // argument is int type
      System.out.println(Math.abs(-9)); // 9
      System.out.println(Math.abs(9)); // 9

      // argument is long type
      System.out.println(Math.abs(-10L)); // 10
      System.out.println(Math.abs(10L)); // 10

      // argument is float type
      System.out.println(Math.abs(-9F)); // 9.0
      System.out.println(Math.abs(9F)); // 9.0

      // argument is double type
      System.out.println(Math.abs(-10.0)); // 10.0
      System.out.println(Math.abs(10.0)); // 10.0
   }
}

If the argument is negative then all of them return the absolute value as the negation of the argument, else the same argument is returned back.

If we import the Math class statically and then we can invoke the abs() method without calling through its class name.

import static java.lang.Math.*;
class Test{
   public static void main(String[] args) {
      System.out.println(abs(-9)); // 9
      System.out.println(abs(9)); // 9
   }
}

The “import static java.lang.Math.*;” statement will import all static members of the Math class. But if we want to import only the abs() method of the Math class, not another static method and variables of Math class then we can use the “import static java.lang.Math.abs;” statement. Learn more about static import in Java

import static java.lang.Math.abs;
class Test{
   public static void main(String[] args) {
      System.out.println(abs(-9)); // 9
      System.out.println(abs(9)); // 9
   }
}

Output:-

9
9

Special Cases while finding absolute value in Java

1) When the passed argument is of int data type and it is equal to the Integer.MIN_VALUE (i.e. -2147483648 or -231) then abs(int a) returns the same value, which is negative.

System.out.println(Math.abs(-2147483648));

Output:- -2147483648

2) Similarly, when the passed value is of long data type and it is equal to Long.MIN_VALUE (i.e. -9223372036854775808 or -263) then abs(long a) returns absolute value as the same value, which is negative.

System.out.println(Math.abs(-9223372036854775808L));

Output:- -9223372036854775808

3) If the argument is positive zero or negative zero, the result is positive zero.

System.out.println(Math.abs(-0)); // 0
System.out.println(Math.abs(0)); // 0
System.out.println(Math.abs(-0.0)); // 0.0
System.out.println(Math.abs(0.0)); // 0.0

Output:-

0
0
0.0
0.0

4) If the argument is infinite, the result is positive infinity.

System.out.println(Math.abs(Double.POSITIVE_INFINITY));
System.out.println(Math.abs(Double.NEGATIVE_INFINITY));

Output:-

Infinity
Infinity

5) If the argument is NaN, the result is NaN.

System.out.println(Math.abs(Double.NaN));
System.out.println(Math.abs(Float.NaN));

Output:-

NaN
NaN

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply