addExact() int in java

addExact(): This function is available in the Math class of Java.

Syntax:

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

This method returns the sum of its arguments, it throwing an exception if the result overflows an int. 

Parameters:

x: the first value.

y: the second value.

Returns: the result.

Note: This method Throws: ArithmeticException - if the result overflows an int.

Approach

Java

public class AddExactInt {
    public static void main(String[] args) {
        int x = 10, y = 12;

        System.out.println(Math.addExact(x, y));

    }
}

Output:

22

No comments:

Post a Comment