negateExact() long in Java

negateExact(): This method is available in the Math class of Java.

Syntax:

long java.lang.Math.negateExact(long a)

This method takes one argument of type long as its parameter. This method returns the negation of the argument.

Note: 

It throwing an exception if the result overflows a long. The overflow only occurs for the minimum value.

Parameters: One parameter is required for this method.

a: the value to negate.

Returns: the result.

Throws: ArithmeticException - if the result overflows a long.

For Example:

Math.negateExact(1010010) = > It returns -1010010.

Approach

Java

public class NegateExactLong {
    public static void main(String[] args) {

        long a = 1010010;
        System.out.println(Math.negateExact(a));

    }
}

Output:

-1010010

No comments:

Post a Comment