Java Math abs() method with Examples

Finding the absolute value of some given number (Java Math abs method)

With this method, we can find the absolute value of some given number. Well, the absolute value is nothing but the distance of that given number from the number 0, on the number line. For example, the absolute value of -5 is 5, which means that the distance of -5 is 5 from the number 0 on the number line. Also, the absolute value of 5 is 5 for the same reason. The method takes the given number as an argument and returns the absolute value of the given number. Let’s have a look at the program, through which we understand the abs method →

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

Executing the above program will give the output as 12.8998998788. Well, this method returns the absolute value of a number. By absolute value, we mean the distance of the number from 0 on the number line. For example, the distance of the number -4 from the number 0 is 4. So, we get the output as 4 if the input is -4.

Special cases –

  • if the input is NaN, the output is also NaN, which stands for Not a Number.
  • If the input is infinity, the output is also infinity. (try giving Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY as an argument to the method).
  • For an argument zero or negative zero, the output is positive zero.

    The method abs are overloaded so as to cover the other primitive types. This means that the method name in the class is the same, just the parameters have changed. The above method takes a double number as input and returns the absolute value, which is again double.

Here are some programs for the other variants of the abs method –

import java.lang.Math;
public class UnderstandingMath {
public static void main(String[] args) {
float variable = -12.23f;
System.out.println(Math.abs(variable));
}
}

The above program shows calculating the absolute value of a float number. The absolute value of the given floating-point value comes out to be 12.23

The next program is for demonstration of the same, but this time for int-type data. Have a look at the following program –

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

Simply executing the above program, we get the output of the above program as 20, which is the absolute value of the given integer value. Now let’s try the same thing for the long-type data.

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

Remember that this number would be by default considered an integer. To make it long, you need to write either ‘l’ or ‘L’ immediately after the number. The same is with the float number.

Executing the above program just gives the output as 209089786789.

Now let’s move forward to another method, which helps us in finding the cube root of a given number.