Java.lang Package Classes

Java Math - abs() Method



The java.lang.Math.abs() method returns the absolute value of a double value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned. In special cases it returns the following:

  • 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.

Syntax

public static double abs(double a)

Parameters

a Specify a number whose absolute value need to be determined.

Return Value

Returns the absolute value (positive value) of the argument.

Exception

NA.

Example:

In the example below, abs() method returns the absolute value of the given double values.

import java.lang.*;

public class MyClass {
 public static void main(String[] args) {
  double x = 10.55;
  double y = -10.55;

  System.out.println(Math.abs(x)); 
  System.out.println(Math.abs(y));   
 }
}

The output of the above code will be:

10.55
10.55

❮ Java.lang - Math