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

Test it Now

Output:

-379

Example 2

Test it Now

Output:

830

Example 3

Test it Now

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

Test it Now

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




Latest Courses