Java Guava | IntMath.checkedAdd(int a, int b) method with Examples Last Updated : 31 Jan, 2019 Comments Improve Suggest changes Like Article Like Report 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 // 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); } } Output: Sum of 25 and 36 is: 61 Sum of 150 and 667 is: 817 Example 2: Java // 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); } } } Output: Reference : https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/IntMath.html#checkedAdd-int-int- Comment More infoAdvertise with us Next Article Java Guava | IntMath.checkedAdd(int a, int b) method with Examples S Sahil_Bansall Follow Improve Article Tags : Java java-guava Guava-Functions Guava-IntMath Practice Tags : Java Similar Reads Java Guava | IntMath.checkedSubtract(int a, int b) method with Examples The checkedSubtract(int a, int b) is a method of Guava's IntMath Class which accepts two parameters a and b, and returns their difference. Syntax: public static int checkedSubtract(int a, int b) Parameters: The method accepts two int values a and b and computes their difference. Return Value: The me 2 min read Java Guava | IntMath.checkedPow(int b, int k) method with Examples checkedPow(int b, int k) is a method of Guava's IntMath Class which accepts two parameters b and k and is used to find the k-th power of b. Syntax: public static int checkedPow(int b, int k) Parameters: The method accepts two parameters, b and k. The parameter b is called base which is raised to the 2 min read Java Guava | IntMath.checkedMultiply(int a, int b) method with Examples The checkedMultiply(int a, int b) is a method of Guava's IntMath Class which accepts two parameters a and b, and returns their product. Syntax: public static int checkedMultiply(int a, int b) Parameters: The method accepts two int values a and b and computes their product. Return Value: The method r 2 min read 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 2 min read Java Guava | LongMath.checkedMultiply(int a, int b) method with Examples The checkedMultiply(long a, long b) is a method of Guava's LongMath Class which accepts two parameters a and b, and returns their product. Syntax: public static long checkedMultiply(long a, long b) Parameters: The method accepts two long values a and b and computes their product. Return Value: The m 2 min read Java Guava | IntMath.divide(int, int, RoundingMode) method with Examples The divide(int p, int q, RoundingMode mode) method of Guava's IntMath Class accepts three parameters and calculates the result of dividing first parameter by second parameter, rounded according to the rounding mode specified by the third parameter. Syntax: public static int divide(int p, int q, Roun 3 min read Java Guava | LongMath.checkedPow(long b, int k) method with Examples checkedPow(long b, long k) is a method of Guava's LongMath Class which accepts two parameters b and k and is used to find the k-th power of b. Syntax: public static long checkedPow(long b, long k) Parameters: The method accepts two parameters, b and k. The parameter b is called base which is raised 2 min read Java Guava | gcd(int a, int b) method of IntMath Class The method gcd(int a, int b) of Guava's IntMath class returns the greatest common divisor of a, b. Syntax : public static int gcd(int a, int b) Where a and b are integers. Return Value : Greatest common divisor of integers a and b. Exceptions : The method gcd(int a, int b) throws IllegalArgumentExce 2 min read Java Guava | Longs.checkedSubtract(long a, long b) method with Examples The checkedSubtract(long a, long b) is a method of Guava's LongMath Class which accepts two parameters a and b, and returns their difference. Syntax: public static long checkedSubtract(long a, long b) Parameters: The method accepts two long values a and b and computes their difference. Return Value: 2 min read DoubleAdder intValue() method in Java with Examples The java.DoubleAdder.intValue() is an inbuilt method in java that returns the sum() as an int after a narrowing primitive conversion. When the object of the class is created its initial value is zero. Syntax: public int intValue() Parameters: This method does not accepts any parameter. Return Value: 1 min read Like