In Java, Math.abs(a) returns the absolute value of a: the same number without its minus sign. Math.abs(-7) is 7, and Math.abs(7) stays 7.
Use it whenever only the size of a number matters: the distance between two values, how far a reading is from a target, or whether two doubles are "close enough" to count as equal.
A static method of the java.lang.Math class, available since the early versions of Java (JDK 1.0). Call it on the class name, as in Math.abs(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass any number. Java picks the overload from the argument type, and the result has the same type.
Java
int steps = Math.abs(-12); // 12
long debt = Math.abs(-5000L); // 5000
double gap = Math.abs(3.5 - 10.0); // 6.5
boolean close = Math.abs(x - y) < 1e-9; // "equal enough" test
Call
What it does
Math.abs(-5)
int overload: returns the int 5.
Math.abs(-5L)
long overload: returns the long 5.
Math.abs(-2.5f)
float overload: returns 2.5f.
Math.abs(a - b)
Distance between a and b on the number line.
byte, short, and char arguments are widened to int first, so they use the int overload and return an int.
Library declarations
Java
// java.lang.Math
public static int abs(int a)
public static long abs(long a)
public static float abs(float a)
public static double abs(double a)
Parameters
Parameter
Description
a
The value whose absolute value you want: an int, long, float, or double.
Return Value
Return Value
Description
int / long
a if it is zero or positive, otherwise -a. Exception:MIN_VALUE comes back unchanged (still negative).
float / double
The value with its sign bit cleared. -0.0 becomes 0.0, -Infinity becomes Infinity, and NaN stays NaN.
Compare with Similar Methods
Methods and idioms that also deal with the sign of a number:
The literal picks the overload.-7 is an int, -7L a long, -7.5f a float, and -7.5 a double; each call returns the same type it received.
Negative zero. Doubles have a separate -0.0. abs clears the sign bit, so it prints as plain 0.0.
Infinity and NaN. Negative infinity becomes positive Infinity, while NaN has no meaningful size and passes through unchanged.
Example 3 — Why abs(Integer.MIN_VALUE) Is Still Negative
The one int that abs cannot make positive, and a simple way around it.
Java
public class MathAbsMinValue {
public static void main(String[] args) {
int smallest = Integer.MIN_VALUE;
System.out.println("abs(MIN_VALUE) = " + Math.abs(smallest));
System.out.println("Still negative? " + (Math.abs(smallest) < 0));
long safe = Math.abs((long) smallest);
System.out.println("As long: " + safe);
}
}
Output
abs(MIN_VALUE) = -2147483648
Still negative? true
As long: 2147483648
How It Works
An uneven range. An int goes from -2147483648 up to only 2147483647, so the positive twin of the smallest value does not exist.
Silent wrap-around. Negating -2147483648 overflows and wraps back to -2147483648, so abs returns a negative number and the check prints true.
Widen first. Casting to long selects the long overload, which has room for 2147483648 and returns the correct positive value.
Frequently Asked Questions
It returns the absolute value of a number: the same value without a minus sign. Math.abs(-7) is 7 and Math.abs(7) is 7.
Yes, in one case per type: Math.abs(Integer.MIN_VALUE) returns -2147483648 and Math.abs(Long.MIN_VALUE) returns -9223372036854775808, because their positive versions do not fit. Use Math.absExact (Java 15+) to get an exception instead.
It returns 0.0. For float and double values, abs clears the sign bit, so negative zero becomes positive zero. NaN stays NaN.
Subtract them and wrap the result in Math.abs, for example Math.abs(a - b). The order of a and b then no longer matters, as long as the subtraction itself does not overflow.
No. Math is in the java.lang package, which every Java file imports automatically.