java.lang.StrictMath addExact​(long x, long y)


The addExact​(long x, long y) method of StrictMath class returns the sum of its arguments, throwing an exception if the result overflows a long.


The addExact​(long x, long y) method of StrictMath class is static thus it should be accessed statically which means the we would be calling this method in this format:

StrictMath.addExact​(long x, long y)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java compare method non statically.

Method Syntax

public static long addExact​(long x, long y)

Method Argument

Data Type Parameter Description
long x the first value
long y the second value

Method Returns

The addExact​(long x, long y) method returns the sum of the arguments.


Requires Java 1.8 and up

Java StrictMath addExact​(long x, long y) Example

Below is a java code demonstrates the use of addExact​(long x, longy) method of StrictMath class. 

Sample Output

Below is the sample output when you run the above example.

Java StrictMath addExact(long x, long y) method example output