Java.lang Package Classes

Java StrictMath - addExact() Method



The java.lang.StrictMath.addExact() method returns sum of its arguments. It throws an exception, if the result overflows a long.

Syntax

 
public static long addExact(long x, long y)  

Parameters

x Specify the first value.
y Specify the second value.

Return Value

Returns sum of its arguments.

Exception

Throws ArithmeticException, if the result overflows a long.

Example:

In the example below, addExact() method is used to add the given numbers.

import java.lang.*;

public class MyClass {
 public static void main(String[] args) {
  long x = 145;
  long y = 139;

  System.out.println(StrictMath.addExact(x, y));    
 }
}

The output of the above code will be:

284

❮ Java.lang - StrictMath