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

Description

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

Notes:

The addExact​(int x, int 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​(int x, int 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 int addExact​(int x, int y)

Method Argument

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

Method Returns

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

Compatibility

Requires Java 1.8 and up

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

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

Sample Output

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

Java StrictMath addExact​(int x, int y) method example output