.abs()

The Math.abs() method returns the absolute value of the argument.

Syntax

Math.abs(n)

Absolute values are the non-negative versions of the value without considering the positive/negative sign.

Example

The following example uses Math.abs() to return the absolute values of -6.5, 2, and -340.8:

class AbsoluteValue {
public static void main(String[] args) {
double x = -6.5;
int y = 2;
double z = -340.8;
System.out.println(Math.abs(x));
System.out.println(Math.abs(y));
System.out.println(Math.abs(z));
}
}

This produces the following output:

6.5
2
340.8

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Java on Codecademy