Java Math negateExact() Method

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

Definition

In Java, Math.negateExact(a) returns -a: the same number with the opposite sign. It behaves exactly like the unary minus, except in one case: for the smallest int or long it throws an ArithmeticException instead of quietly returning a negative number.

That one case exists because the number ranges are lopsided: an int goes down to -2147483648 but up only to 2147483647, so +2147483648 simply does not exist.

A static method of the java.lang.Math class, added in Java 8 (JDK 1.8). Call it on the class name, as in Math.negateExact(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass an int or a long; the result has the same type.

Java
int a = Math.negateExact(5);                      // -5
long b = Math.negateExact(-5_000_000_000L);        // 5000000000
Math.negateExact(Integer.MIN_VALUE);               // throws ArithmeticException
CallWhat it does
Math.negateExact(9)int overload: returns -9.
Math.negateExact(-9L)long overload: returns 9L.
Math.negateExact(Integer.MIN_VALUE + 1)Returns 2147483647: the biggest value that can be negated safely.
Math.negateExact(Integer.MIN_VALUE)Throws ArithmeticException ("integer overflow").

Zero is its own negative: Math.negateExact(0) is 0. There is no double version, because negating a double only flips its sign bit and can never overflow.

Library declarations
Java
// java.lang.Math
public static int negateExact(int a)
public static long negateExact(long a)

Parameters

ParameterDescription
aThe int or long whose sign you want to flip.

Return Value

Return ValueDescription
int / long-a, when it fits in the type.
Throws ArithmeticExceptionOnly for Integer.MIN_VALUE (integer overflow) and Long.MIN_VALUE (long overflow).

Compare with Similar Methods

Sign-changing options and how they handle MIN_VALUE:

MethodHow it differs
-aUnary minus: -Integer.MIN_VALUE silently stays -2147483648.
absExact(a)Makes a value non-negative and throws for MIN_VALUE. Java 15+.
abs(a)Same trap as unary minus: MIN_VALUE comes back unchanged.
subtractExact(0, a)Gives the same result and the same exception as negateExact(a).

Java Examples

Example 1 — Positive, Negative, and Zero

Normal values flip sign exactly as they do with the unary minus.

Java
public class MathNegateExactBasics {
    public static void main(String[] args) {
        System.out.println(Math.negateExact(5));
        System.out.println(Math.negateExact(-9));
        System.out.println(Math.negateExact(0));
        System.out.println(Math.negateExact(-5_000_000_000L));
    }
}

How It Works

  1. Positive to negative. 5 becomes -5, and -9 becomes 9; the sign flips and the size stays the same.
  2. Zero stays zero. There is no negative integer zero, so negateExact(0) is simply 0.
  3. A long value. -5000000000 is too big for an int, so the long overload runs and returns 5000000000.

Example 2 — Why -Integer.MIN_VALUE Is Still Negative

Compare plain negation, negateExact, and the biggest value that can be negated safely.

Java
public class MathNegateExactMinValue {
    public static void main(String[] args) {
        int smallest = Integer.MIN_VALUE;
        System.out.println("plain -:       " + (-smallest));
        try {
            System.out.println("negateExact:   " + Math.negateExact(smallest));
        } catch (ArithmeticException e) {
            System.out.println("int threw:     " + e.getMessage());
        }
        System.out.println("MIN + 1:       " + Math.negateExact(smallest + 1));
        try {
            Math.negateExact(Long.MIN_VALUE);
        } catch (ArithmeticException e) {
            System.out.println("long threw:    " + e.getMessage());
        }
    }
}

How It Works

  1. No positive twin. +2147483648 is one more than the largest int, so negating the smallest one wraps back to -2147483648 and the plain minus prints a negative number.
  2. The exact version objects. negateExact sees that the answer cannot be represented and throws integer overflow; the line starting "negateExact:" is never printed.
  3. One step away is safe. -2147483647 negates to 2147483647, the largest int. The long pair behaves the same way and reports long overflow.

Example 3 — Reverse Ledger Entries and Reject a Bad One

Flip the sign of every entry to undo it. A corrupted entry is reported instead of being "reversed" into a wrong value.

Java
public class MathNegateExactLedger {
    public static void main(String[] args) {
        long[] entries = {2500L, -1800L, Long.MIN_VALUE, 0L};
        for (long entry : entries) {
            try {
                System.out.println(entry + " -> " + Math.negateExact(entry));
            } catch (ArithmeticException e) {
                System.out.println("cannot reverse " + entry + ": " + e.getMessage());
            }
        }
    }
}

How It Works

  1. Credits and debits swap. A deposit of 2500 reverses to -2500, and a withdrawal of -1800 reverses to 1800.
  2. The impossible entry. Long.MIN_VALUE has no positive long counterpart, so negateExact throws and the catch block reports it; unary minus would have left the entry negative and the "undo" would have done nothing.
  3. The loop carries on. The exception only ends the current try, so the next entry, 0, is still processed and prints 0 -> 0.

Frequently Asked Questions

It returns the negative of an int or long, like the unary minus, but throws ArithmeticException for Integer.MIN_VALUE and Long.MIN_VALUE instead of returning a wrong value.
Because the int range is -2147483648 to 2147483647. The positive value 2147483648 does not fit, so the negation wraps around and gives -2147483648 again.
Only Integer.MIN_VALUE (message "integer overflow") and Long.MIN_VALUE (message "long overflow"). Every other int and long value can be negated.
No. Negating a double just flips its sign bit and cannot overflow, so the plain minus operator is already safe.
Both fail only for MIN_VALUE. negateExact flips the sign of any value, while absExact (Java 15+) makes a value non-negative.

Next: nextAfter()

The neighbouring float or double in the direction of a second value.

nextAfter() 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