Java – Find Absolute Value of Double – abs() Method with Examples

java.lang.Math.abs() method is used to find the absolute value of a double number for the given input (x – parameter) in Java. If argument is not negative, the argument is returned as it is, but if the argument is negative then the negation of argument is returned.

Package

abs() method exists in Math class of java.lang package.
Since: JDK1.0

 

Declaration of abs() Method

The declaration of abs() method is:

public static double abs(double x)

 

Syntax of abs() Method

The syntax of abs() method in Java is:

 
java.lang.Math.abs(double x)

Example

/*
*Find the absolute value of given input(double) without importing the
*java.lang Package.
*/
public class JavaMathExample {
    public static void main(String[] args) {
        //Declaring variables
        double x = -632.432516;
        double result;
        //Assign absolute value of x to result variable
        result = java.lang.Math.abs(x);
        //Printing result to the console
        System.out.println(result);
    }
}

Output:
632.432516

Example
By importing java.lang package the syntax of abs() method can be used as:

/*
*Find the absolute value of given input(double) by importing the java.lang Package.
*/
import java.lang.*;  //importing package

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

        //Declaring variables
        double x = -385.723;
        double result;

        //Assign absolute value of x to result variable
        result = Math.abs(x);

        //Printing result to the console
        System.out.println(result);
    }
}

Output:
385.723

 

Special Cases of abs() Method in Java

If the argument is positive zero or negative zero, the result is positive zero.
If the argument is infinite, the result is positive infinity.
If the argument is NaN, the result is NaN.
Example

/*
*This example shows some special cases of abs() method in Java
*/
import java.lang.*; //importing java.lang package

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

        //Declaring variables
        double positiveZero = 0;
        double negativeZero = -0;
        double positiveInfinity = Double.POSITIVE_INFINITY;
        double negativeInfinity = Double.NEGATIVE_INFINITY;
        double nan = Double.NaN;
        double result;

        //Here argument is positive zero, output will also be positive zero
        result = Math.abs(positiveZero);
        System.out.println(result);

        //Here argument is negative zero, output will be positive zero
        result = Math.abs(negativeZero);
        System.out.println(result);

        //Here argument is positive infinity, output will also be positive infinity
        result = Math.abs(positiveInfinity);
        System.out.println(result);

        //Here argument is negative infinity, output will be negative infinity
        result = Math.abs(negativeInfinity);
        System.out.println(result);

        //Here argument is NaN, output will be NaN
        result = Math.abs(nan);
        System.out.println(result);


    }

}

Output:
0.0
0.0
Infinity
Infinity
NaN

 

Parameters of abs() Method in Java

x – Where x is any valid double input value. This parameter is required.

 

Error Handling

If the x parameter is not a number abs() method returns an error.
If there is no argument (x – input value) passes to the method, the compiler will produce an error.
Example

/*
*This example returns a compile time error
*/
import java.lang.*; //importing java.lang package

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

        //Declaring variables
        String x = "Not a number";
        double result;

        //argument is not a number
        result = Math.abs(x);
        System.out.println(result);

        //Here no argument is used
        result = Math.abs();
        System.out.println(result);


    }

}

Output:
Error

 

Return Value of abs() Method in Java

abs() method returns the absolute value of a double number for the given input (x – parameter).

 

Java abs() Method Example 1

import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {
        double negativeValue = -367.344d;
        double positiveValue = 6258.3825d;
        double zero = 0;
        System.out.println("absolute value of a Negative Value: "+Math.abs(negativeValue));
        System.out.println("absolute value of a Positive Value: "+Math.abs(positiveValue));
        System.out.println("absolute value of Zero: "+Math.abs(zero));

    }
}

Output:
absolute value of a Negative Value: 367.344
absolute value of a Positive Value: 6258.3825
absolute value of Zero: 0.0

 

Java abs() Method Example 2

import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {
        //Declare and initialize an array containing double values
        double test [] = { Double.MAX_VALUE, Double.MIN_VALUE,
                Double.NaN, Double.MIN_EXPONENT, 1839.3666e-15, 351.7569, 0, -7.4444, -2113.7294e3 };

        //Use for loop to print absolute value of each element of the array
        for (int i=0; i < test.length; i++) {
            System.out.println(  Math.abs(test[i])  );
        }

    }
}

Output:
1.7976931348623157E308
4.9E-324
NaN
1022.0
1.8393666E-12
351.7569
0.0
7.4444
2113729.4