Java Math - abs() Method
The java.lang.Math.abs() method returns the absolute value of a long 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 equal to the value of Long.MIN_VALUE, the most negative representable long value, the result is that same value, which is negative.
Syntax
public static long abs(long 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 long values.
import java.lang.*; public class MyClass { public static void main(String[] args) { long x = 1234; long y = -1234; System.out.println(Math.abs(x)); System.out.println(Math.abs(y)); } }
The output of the above code will be:
1234 1234
❮ Java.lang - Math