Java Math absExact() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 15+

Definition

In Java, Math.absExact(a) returns the absolute value of an int or long, just like Math.abs, with one difference: when the result cannot be represented, it throws ArithmeticException instead of quietly returning a wrong answer.

That only happens for Integer.MIN_VALUE and Long.MIN_VALUE, whose positive versions do not fit in their type. Use absExact when a wrong sign would be a real bug, for example in money or measurement code.

A static method of the java.lang.Math class, added in Java 15 (JDK 15). Call it on the class name, as in Math.absExact(...). No import is needed, because classes in java.lang are always visible. Requires Java 15 or newer.

Syntax

Pass an int or a long. Wrap the call in try if MIN_VALUE can reach it.

Java
int a = Math.absExact(-250);               // 250
long b = Math.absExact(-9_000_000_000L);   // 9000000000
Math.absExact(Integer.MIN_VALUE);          // throws ArithmeticException
CallWhat it does
Math.absExact(-42)Returns the int 42.
Math.absExact(-42L)Returns the long 42.
Math.absExact(Integer.MIN_VALUE)Throws ArithmeticException.
Math.absExact((long) Integer.MIN_VALUE)Returns 2147483648 as a long.

There are no float or double versions: floating-point abs can never overflow, so plain Math.abs is already safe for them.

Library declarations
Java
// java.lang.Math (Java 15+)
public static int absExact(int a)
public static long absExact(long a)

Parameters

ParameterDescription
aThe int or long whose absolute value you want.

Return Value

Return ValueDescription
int / longa if it is zero or positive, otherwise -a. Always zero or positive.
Throws ArithmeticExceptionWhen a is Integer.MIN_VALUE or Long.MIN_VALUE.

Compare with Similar Methods

How the safe version relates to its neighbours:

MethodHow it differs
abs(a)Never throws; returns a negative MIN_VALUE unchanged.
negateExact(a)Always flips the sign; throws for MIN_VALUE too.
toIntExact(v)Another overflow guard: a long to int conversion that throws.
addExact(a, b)Same idea (throw on overflow) for addition.

Java Examples

Example 1 — Everyday Values Work Just Like abs()

For every value except MIN_VALUE, absExact gives exactly what abs gives. This example needs Java 15 or newer.

Java
public class MathAbsExactBasics {
    public static void main(String[] args) {
        System.out.println(Math.absExact(-250));
        System.out.println(Math.absExact(42));
        System.out.println(Math.absExact(0));
        System.out.println(Math.absExact(-9_000_000_000L));
    }
}

How It Works

  1. Negative in, positive out. -250 loses its sign and prints 250, exactly as Math.abs(-250) would.
  2. Zero and positives pass through. 42 and 0 are already non-negative, so they come back unchanged.
  3. The long overload. The L suffix makes -9_000_000_000 a long (it is too big for an int), so the long version returns 9000000000.

Example 2 — MIN_VALUE: Wrong Answer vs Exception

Run both methods on Integer.MIN_VALUE to see the difference. Java 15+.

Java
public class MathAbsExactMinValue {
    public static void main(String[] args) {
        int smallest = Integer.MIN_VALUE;
        System.out.println("abs:      " + Math.abs(smallest));
        try {
            System.out.println("absExact: " + Math.absExact(smallest));
        } catch (ArithmeticException e) {
            System.out.println("absExact threw: " + e.getMessage());
        }
    }
}

How It Works

  1. The quiet failure. Math.abs cannot represent +2147483648 in an int, so it hands back the negative input unchanged.
  2. The loud failure. Math.absExact detects the same case and throws before the second println can run.
  3. Readable message. The catch block prints getMessage(), which names the exact problem: the absolute value of Integer.MIN_VALUE overflows.

Example 3 — Find the Biggest Swing and Reject Bad Data

A list of daily account changes contains one corrupt value. absExact flags it instead of letting a negative "size" slip into the result. Java 15+.

Java
public class MathAbsExactSwings {
    public static void main(String[] args) {
        long[] changes = {1200L, -3400L, 560L, Long.MIN_VALUE};
        long biggest = 0;
        for (long c : changes) {
            try {
                long size = Math.absExact(c);
                biggest = Math.max(biggest, size);
                System.out.println(c + " -> " + size);
            } catch (ArithmeticException e) {
                System.out.println(c + " -> rejected (" + e.getMessage() + ")");
            }
        }
        System.out.println("Biggest swing: " + biggest);
    }
}

How It Works

  1. Size of each change. Gains and losses both count as swings, so -3400 becomes 3400 and is compared with Math.max.
  2. Corrupt entry caught. Long.MIN_VALUE has no positive long twin, so absExact throws and the loop prints a rejection instead.
  3. Result stays trustworthy. With plain abs the bad value would stay negative and silently lose every comparison; here the biggest real swing, 3400, is reported.

Frequently Asked Questions

They return the same value for every input except MIN_VALUE. For Integer.MIN_VALUE or Long.MIN_VALUE, abs returns the negative input unchanged, while absExact throws ArithmeticException.
Java 15. On older versions, compare the value with Integer.MIN_VALUE yourself, or cast to long and call Math.abs.
Overflow to represent absolute value of Integer.MIN_VALUE for the int version, and the same text ending in Long.MIN_VALUE for the long version.
No. Floating-point absolute value cannot overflow, so Math.abs(double) is already safe.
No, it is an unchecked exception, so the compiler does not require a try block. Catch it only where you can do something useful, such as rejecting bad input.

Next: acos()

Arc cosine: the angle in radians (0 to pi) whose cosine is the given value.

acos() 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.

8 people found this page helpful