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

NameTypeDescription
xintInteger operand.
yintInteger 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));    }}
Java Reference