subtractExact() int in Java

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

Syntax:

int java.lang.Math.subtractExact(int x, int y)

This method takes two arguments of type int as its parameters. This method returns the difference of the arguments, throwing an exception if the result overflows an int. 

Parameters: Two parameters are required for this method.

x: the first value.

y: the second value to subtract from the first.

Returns: the result.

Throws: ArithmeticException - if the result overflows an int.

For Example:

Math.subtractExact(56,12) = > It returns  44.

Approach

Java

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

        int x = 56, y = 12;
        System.out.println(Math.subtractExact(x, y));

    }
}

Output:

44

No comments:

Post a Comment