91
Java Math.negateExact() method
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:
-379
Example 2
Output:
830
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)
Next TopicJava Math