Java SE API
Math.addExact(int, int)
In java.lang.Math (java.base), adds two int values and throws ArithmeticException if the mathematical sum is outside int range. It does not automatically widen the result. No import is required.
Formal Syntax & Structure
java syntaxjava
public static int addExact(int x, int y)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| x | int | Integer operand. |
| y | int | Integer operand. |
Return Value
int
Exact sum if representable as int.
Working Example
Example Snippetjava
public class Main { public static void main(String[] args) { System.out.println(Math.addExact(25, 30)); }}