Java.lang Package Classes

Java Math - addExact() Method



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

Syntax

public static int addExact(int x, int 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 an int.

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) {
  int x = 12;
  int y = 17;
  
  System.out.println(Math.addExact(x, y));   
 }
}

The output of the above code will be:

29

❮ Java.lang - Math