Java.lang Package Classes

Java Math - negateExact() Method



The java.lang.Math.negateExact() method returns the negation of the argument. It throws an exception, if the result overflows an int.

Syntax

public static int negateExact(int x)  

Parameters

x Specify the value.

Return Value

Returns the negation of the argument.

Exception

Throws ArithmeticException, if the result overflows an int.

Example:

In the example below, negateExact() method is used to negate the argument.

import java.lang.*;

public class MyClass {
 public static void main(String[] args) {
  int i = 5;
  int j = -2;

  System.out.println(Math.negateExact(i)); 
  System.out.println(Math.negateExact(j));
 }
}

The output of the above code will be:

-5
2

❮ Java.lang - Math