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
Call
What 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
Parameter
Description
a
The int or long whose sign you want to flip.
Return Value
Return Value
Description
int / long
-a, when it fits in the type.
Throws ArithmeticException
Only 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:
plain -: -2147483648
int threw: integer overflow
MIN + 1: 2147483647
long threw: long overflow
How It Works
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.
The exact version objects.negateExact sees that the answer cannot be represented and throws integer overflow; the line starting "negateExact:" is never printed.
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.
Credits and debits swap. A deposit of 2500 reverses to -2500, and a withdrawal of -1800 reverses to 1800.
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.
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.