Close

Java - Math.addExact() Examples

Java Java API 


Class:

java.lang.Math

java.lang.Objectjava.lang.Objectjava.lang.Mathjava.lang.MathLogicBig

Methods:

public static int addExact(int x,
                           int y)

Returns the sum of its arguments, throwing an exception if the result overflows an int.



public static long addExact(long x,
                            long y)

Returns the sum of its arguments, throwing an exception if the result overflows a long.


Examples


package com.logicbig.example.math;

public class AddExactExample {

public static void main(String... args) {
int x = 100;
System.out.println("First Integer: " + x);
int y = 300;
System.out.println("Second Integer: " + y);
int result = Math.addExact(x, y);
System.out.println("Result: " + result);
}
}

Output

First Integer: 100
Second Integer: 300
Result: 400




package com.logicbig.example.math;

public class AddExactExample2 {

public static void main(String... args) {
long x = Long.MAX_VALUE - 3;
for (long y = 1; y <= 5; y++) {
System.out.println("-----------");
System.out.println("Result without Math.addExact: ");
System.out.printf("%s + %s = %s%n", x, y, x + y);
System.out.println("Result with Math.addExact: ");
try {
x = Math.addExact(x, y);
} catch (ArithmeticException e) {
System.out.println("error: " + e);
continue;
}
System.out.println(x);
}
}
}

Output

-----------
Result without Math.addExact:
9223372036854775804 + 1 = 9223372036854775805
Result with Math.addExact:
9223372036854775805
-----------
Result without Math.addExact:
9223372036854775805 + 2 = 9223372036854775807
Result with Math.addExact:
9223372036854775807
-----------
Result without Math.addExact:
9223372036854775807 + 3 = -9223372036854775806
Result with Math.addExact:
error: java.lang.ArithmeticException: long overflow
-----------
Result without Math.addExact:
9223372036854775807 + 4 = -9223372036854775805
Result with Math.addExact:
error: java.lang.ArithmeticException: long overflow
-----------
Result without Math.addExact:
9223372036854775807 + 5 = -9223372036854775804
Result with Math.addExact:
error: java.lang.ArithmeticException: long overflow




Unhandled overflow exception:

package com.logicbig.example.math;

public class AddExactExample3 {

public static void main(String... args) {
int x = Integer.MAX_VALUE;
int y = 10000;
int sum = Math.addExact(x, y);
System.out.printf("%s%n", sum);
}
}

Output

java.lang.ArithmeticException: integer overflow
at java.lang.Math.addExact (Math.java:828)
at com.logicbig.example.math.AddExactExample3.main (AddExactExample3.java:14)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:564)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:282)
at java.lang.Thread.run (Thread.java:832)




See Also