Java Math abs() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Math

Definition

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
CallWhat 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

ParameterDescription
aThe value whose absolute value you want: an int, long, float, or double.

Return Value

Return ValueDescription
int / longa if it is zero or positive, otherwise -a. Exception: MIN_VALUE comes back unchanged (still negative).
float / doubleThe 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:

MethodHow it differs
absExact(a)Same result, but throws ArithmeticException for MIN_VALUE. Java 15+.
signum(d)Returns only the sign (-1.0, 0.0, or 1.0), not the size.
copySign(m, s)Sets a chosen sign instead of removing it.
a < 0 ? -a : aThe hand-written version: same MIN_VALUE trap, harder to read.

Java Examples

Example 1 — Distance Between Values and a Tolerance Check

Find how many degrees apart two temperatures are, then use abs to compare two doubles safely.

Java
public class MathAbsDistance {
    public static void main(String[] args) {
        int morning = -4;
        int afternoon = 9;
        int change = morning - afternoon;
        System.out.println("Raw change: " + change);
        System.out.println("Degrees apart: " + Math.abs(change));

        double sum = 0.1 + 0.2;
        System.out.println("sum == 0.3? " + (sum == 0.3));
        System.out.println("close to 0.3? " + (Math.abs(sum - 0.3) < 1e-9));
    }
}

How It Works

  1. Order no longer matters. -4 minus 9 is -13; Math.abs drops the sign, so the answer is 13 degrees whichever reading you subtract first.
  2. Why == fails. 0.1 and 0.2 are stored as binary fractions that are slightly off, so their sum is 0.30000000000000004, not the double closest to 0.3.
  3. Compare with a tolerance. The gap Math.abs(sum - 0.3) is about 5.5E-17, far below 1e-9, so the "close enough" test prints true.

Example 2 — One Method, Four Types, and the Special Values

Call each overload, then see what happens to negative zero, infinity, and NaN.

Java
public class MathAbsTypes {
    public static void main(String[] args) {
        System.out.println("int:    " + Math.abs(-7));
        System.out.println("long:   " + Math.abs(-7L));
        System.out.println("float:  " + Math.abs(-7.5f));
        System.out.println("double: " + Math.abs(-7.5));
        System.out.println("-0.0:   " + Math.abs(-0.0));
        System.out.println("-Inf:   " + Math.abs(Double.NEGATIVE_INFINITY));
        System.out.println("NaN:    " + Math.abs(Double.NaN));
    }
}

How It Works

  1. 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.
  2. Negative zero. Doubles have a separate -0.0. abs clears the sign bit, so it prints as plain 0.0.
  3. 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);
    }
}

How It Works

  1. An uneven range. An int goes from -2147483648 up to only 2147483647, so the positive twin of the smallest value does not exist.
  2. Silent wrap-around. Negating -2147483648 overflows and wraps back to -2147483648, so abs returns a negative number and the check prints true.
  3. 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.

Next: absExact()

Absolute value that throws ArithmeticException instead of returning a negative MIN_VALUE (Java 15+).

absExact() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful