.negateExact()

The Math.negateExact() method returns the negation of the argument and raises an exception if the result overflows.

Syntax

Math.negateExact(num)

The num parameter can be of type int or long.

Example

The following example demonstrates using the .negateExact() method:

// Test.java
public class Test {
public static void main(String args[]) {
int num_int = 12;
long num_long = 123L;
System.out.println(Math.negateExact(num_int));
System.out.println(Math.negateExact(num_long));
}
}

This results in the following output:

-12
-123

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Java on Codecademy