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 a long.

Syntax

 
public static long negateExact(long x)  

Parameters

x Specify the value.

Return Value

Returns the negation of the argument.

Exception

Throws ArithmeticException, if the result overflows a long.

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) {
  long i = 145;
  long j = -150;

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

The output of the above code will be:

-145
150

❮ Java.lang - Math