The checkedAdd(int a, int b) is a method of Guava’s IntMath Class which accepts two parameters a and b, and returns their sum.
Syntax:
public static int checkedAdd(int a, int b)
Parameters: The method accepts two int values a and b and computes their sum.
Return Value: The method returns the sum of int values passed to it, provided it does not overflow.
Exceptions: The method checkedAdd(int a, int b) throws ArithmeticException if the sum i.e, (a – b) overflows in signed int arithmetic.
Below examples illustrate the implementation of above method:
Example 1:
// Java code to show implementation of // checkedAdd(int a, int b) method // of Guava's IntMath class import java.math.RoundingMode; import com.google.common.math.IntMath; class GFG { // Driver code public static void main(String args[]) { int a1 = 25; int b1 = 36; // Using checkedAdd(int a, int b) // method of Guava's IntMath class int ans1 = IntMath.checkedAdd(a1, b1); System.out.println("Sum of " + a1 + " and " + b1 + " is: " + ans1); int a2 = 150; int b2 = 667; // Using checkedAdd(int a, int b) // method of Guava's IntMath class int ans2 = IntMath.checkedAdd(a2, b2); System.out.println("Sum of " + a2 + " and " + b2 + " is: " + ans2); } } |
Sum of 25 and 36 is: 61 Sum of 150 and 667 is: 817
Example 2:
// Java code to show implementation of // checkedAdd(int a, int b) method // of Guava's IntMath class import java.math.RoundingMode; import com.google.common.math.IntMath; class GFG { static int findDiff(int a, int b) { try { // Using checkedAdd(int a, int b) method // of Guava's IntMath class // This should throw "ArithmeticException" // as the sum overflows in signed // int arithmetic int ans = IntMath.checkedAdd(a, b); // Return the answer return ans; } catch (Exception e) { System.out.println(e); return -1; } } // Driver code public static void main(String args[]) { int a = Integer.MIN_VALUE; int b = 452; try { // Function calling findDiff(a, b); } catch (Exception e) { System.out.println(e); } } } |
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java Guava | Longs.contains() method with Examples
- Java Guava | Floats.contains() method with Examples
- Java Guava | Longs.max() method with Examples
- Java Guava | Chars.max() method with Examples
- Java Guava | Shorts.max() method with Examples
- Java Guava | Chars.contains() method with Examples
- Java Guava | Doubles.max() method with Examples
- Java Guava | Chars.min() method with Examples
- Java Guava | Floats.min() method with Examples
- Java Guava | Shorts.contains() method with Examples
- Java Guava | Floats.max() method with Examples
- Java Guava | Bytes.contains() method with Examples
- Java Guava | Booleans.contains() method with Examples
- Java Guava | Doubles.contains() method with Examples
- Java Guava | Doubles.min() method with Examples
- Java Guava | Shorts.min() method with Examples
- Java Guava | Longs.min() method with Examples
- Java Guava | Lists.reverse() method with Examples
- Java Guava | Lists.partition() method with Examples
- Java Guava | IntMath.checkedMultiply(int a, int b) method with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

