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 method returns the product of long values passed to it, provided it does not overflow.
Exceptions: The method checkedMultiply(long a, long b) throws ArithmeticException if the product 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 // checkedMultiply(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 checkedMultiply(long a, long b) // method of Guava's LongMath class long ans1 = LongMath.checkedMultiply(a1, b1); System.out.println("Product of " + a1 + " and " + b1 + " is: " + ans1); long a2 = 150; long b2 = 667; // Using checkedMultiply(long a, long b) // method of Guava's LongMath class long ans2 = LongMath.checkedMultiply(a2, b2); System.out.println("Product of " + a2 + " and " + b2 + " is: " + ans2); } } |
Product of 25 and 36 is: 900 Product of 150 and 667 is: 100050
Example 2:
// Java code to show implementation of // checkedMultiply(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 checkedMultiply(long a, long b) method // of Guava's LongMath class // This should throw "ArithmeticException" // as the product overflows in signed // long arithmetic long ans = LongMath.checkedMultiply(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); } } } |
java.lang.ArithmeticException: overflow
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- TimeZone getOffset(int, int, int, int, int, int) Method in Java with Examples
- Java Guava | IntMath.checkedSubtract(int a, int b) method with Examples
- Java Guava | IntMath.checkedMultiply(int a, int b) method with Examples
- Java Guava | IntMath.checkedAdd(int a, int b) method with Examples
- Java Guava | Ints.indexOf(int[] array, int[] target) method with Examples
- Java Guava | IntMath.checkedPow(int b, int k) method with Examples
- Java Guava | IntMath.divide(int, int, RoundingMode) method with Examples
- Java Guava | binomial(int n, int k) of LongMath Class with Examples
- ZoneOffset ofHoursMinutesSeconds(int, int, int) method in Java with Examples
- SimpleTimeZone setStartRule(int, int, int) method in Java with Examples
- SimpleTimeZone setEndRule(int, int, int) method in Java with Examples
- Java Guava | pow(int b, int k) method of IntMath Class
- Java Guava | gcd(int a, int b) method of IntMath Class
- HijrahDate of(int, int, int) method in Java with Example
- IsoChronology date(int, int, int) method in Java with Example
- JapaneseChronology date(int, int, int) method in Java with Example
- JapaneseDate of(int, int, int) method in Java with Example
- JapaneseDate of(JapaneseEra,int, int, int) method in Java with Example
- MinguoChronology date(int, int, int) method in Java with Example
- MinguoDate of(int, int, int) method in Java with Example
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.

