Java Guava | LongMath.checkedAdd(long a, long b) method with Examples
The checkedAdd(long a, long b) is a method of Guava’s LongMath Class which accepts two parameters a and b, and returns their sum.
Syntax:
public static long checkedAdd(long a, long b)
Parameters: The method accepts two long values a and b and computes their sum.
Return Value: The method returns the sum of long values passed to it, provided it does not overflow.
Exceptions: The method checkedAdd(long a, long b) throws ArithmeticException if the sum i.e, (a – b) overflows in signed long arithmetic.
Below examples illustrate the implementation of above method:
Example 1:
// Java code to show implementation of // checkedAdd(long a, long b) method // of Guava's LongMath class import java.math.RoundingMode; import com.google.common.math.LongMath; class GFG { // Driver code public static void main(String args[]) { long a1 = 25; long b1 = 36; // Using checkedAdd(long a, long b) // method of Guava's LongMath class long ans1 = LongMath.checkedAdd(a1, b1); System.out.println("Sum of " + a1 + " and " + b1 + " is: " + ans1); long a2 = 150; long b2 = 667; // Using checkedAdd(long a, long b) // method of Guava's LongMath class long ans2 = LongMath.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(long a, long b) method // of Guava's LongMath class import java.math.RoundingMode; import com.google.common.math.LongMath; class GFG { static long findDiff(long a, long b) { try { // Using checkedAdd(long a, long b) method // of Guava's LongMath class // This should throw "ArithmeticException" // as the sum overflows in signed // long arithmetic long ans = LongMath.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[]) { long a = Long.MIN_VALUE; long b = 452; try { // Function calling findDiff(a, b); } catch (Exception e) { System.out.println(e); } } } |
Recommended Posts:
- Java Guava | LongMath.divide(long, long, RoundingMode) method with Examples
- Java Guava | Longs.checkedSubtract(long a, long b) method with Examples
- Java Guava | Longs.indexOf(long[] array, long target) method with Examples
- Java Guava | Longs.indexOf(long[] array, long[] target) method with Examples
- Java Guava | LongMath.checkedPow(long b, int k) method with Examples
- Java Guava | mod(long x, long m) of LongMath Class with Examples
- Java Guava | mean(long x, long y) of LongMath Class with Examples
- Java Guava | gcd(long a, long b) of LongMath Class with Examples
- Java Guava | LongMath log10(long x, RoundingMode mode) method with Examples
- Java Guava | pow(long b, int k) of LongMath Class with Examples
- Java Guava | isPowerOfTwo(long x) of LongMath Class with Examples
- Java Guava | log2(long x, RoundingMode mode) of LongMath Class with Examples
- Java Guava | sqrt(long x, RoundingMode mode) of LongMath Class with Examples
- Duration ofSeconds(long, long) method in Java with Examples
- Java Long decode() 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.



