negateExact() int in Java

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

Syntax:

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

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

Note:

It throwing an exception if the result overflows an int.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 an int.

For Example:

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

Approach

Java

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

        int a = 2021;
        System.out.println(Math.negateExact(a));

    }
}

Output:

-2021

No comments:

Post a Comment