addExact() long in Java

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

Syntax:

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

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

Parameters: Two parameters are required for this function.

x: the first value.

y: the second value. Returns: the result.

Note: Throws:ArithmeticException - if the result overflows a long.

Approach

Java

public class AddExactLong {
    public static void main(String[] args) {
        long x = 188817, y = 138282;
        System.out.println(Math.addExact(x, y));
        
    }
}

Output:

327099

No comments:

Post a Comment