Math.negateExact()
Java Math.negateExact() method
Last Updated : 17 Mar 2025
The java.lang.Math.negateExact() returns the negation of the argument. It will throw an exception if the result overflows either int or long.
Syntax
Parameter
Return
- If the argument is Integer.MIN_VALUE or Long.MIN_VALUE, it will throw an ArithmeticException.
Example 1
Output:
Example 2
Output:
Example 3
Output:
Exception in thread "main" java.lang.ArithmeticException: integer overflow at java.lang.Math.negateExact(Math.java:977) at negateExactExample3.main(negateExactExample3.java:7)
Example 4
Output:
Exception in thread "main" java.lang.ArithmeticException: long overflow at java.lang.Math.negateExact(Math.java:994) at negateExactExample4.main(negateExactExample4.java:7)