Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
addexact
math
communitycreator

What is Math.addExact() in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The addExact method in Java adds its two passed arguments and returns the sum as a result. The arguments will be either int or long. addExact is a static method present in the Math class.

Syntax

public static long addExact(long a,long b);

public static long addExact(int a, int b);

Arguments

  • a: First value (int/long).
  • b: Second value(int/long).

Return value

Returns the sum of a and b.

Example

import java.lang.Math;
class AddExactTest {
public static void main(String cmdArgs[]) {
int a = 10;
int b = 10;
int sum = Math.addExact(a, b);
System.out.print("The sume of "+ a + " and " + b + " is ");
System.out.println(sum);
long la = 100000000l;
long lb = 100000000l;
long lsum = Math.addExact(la, lb);
System.out.print("The sume of "+ la + " and " + lb + " is ");
System.out.println(lsum);
}
}

In the code above:

  • We created four variables, a and b, of the int and la type, and lb of the long type.

  • We called the Math.addExact method with a and b as arguments. This will return the sum of a and b as a result.

  • Again, we called the Math.addExact method with la and lb as arguments. This will return the sum of la and lb as a result.


The addExact method will throw ArithmeticException if the sum of the arguments is greater than the maximum value type the arguments can hold.

Example

For example, if the two arguments are int, the ArithmeticException will be thrown if the sum of two int arguments is greater than the maximum value int type can hold.

import java.lang.Math;
class AddExactTest {
public static void main(String cmdArgs[]) {
try{
int a = Integer.MAX_VALUE;
int b = 1;
int sum = Math.addExact(a, b);
}catch(Exception e){
System.out.println(e);
}
}
}

In the code above, we created two variables, a and b, of the int type. We assigned the maximum value an integer can hold to the variable a, and 1 to the variable b.

When we call the addExact method with a and b as arguments, we will get ArithmeticException, because the sum of a and b is greater than the value an integer can hold.

RELATED TAGS

java
addexact
math
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring