Sunday, February 15, 2026
HomeLanguagesJavaJava Guava | IntMath.checkedMultiply(int a, int b) method with Examples

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 returns the product of int values passed to it, provided it does not overflow.

Exceptions: The method checkedMultiply(int a, int b) throws ArithmeticException if the product 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
// checkedMultiply(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 checkedMultiply(int a, int b)
        // method of Guava's IntMath class
        int ans1 = IntMath.checkedMultiply(a1, b1);
  
        System.out.println("Product of " + a1 + " and "
                           + b1 + " is: " + ans1);
  
        int a2 = 150;
        int b2 = 667;
  
        // Using checkedMultiply(int a, int b)
        // method of Guava's IntMath class
        int ans2 = IntMath.checkedMultiply(a2, b2);
  
        System.out.println("Product of " + a2 + " and "
                           + b2 + " is: " + ans2);
    }
}


Output:

Product of 25 and 36 is: 900
Product of 150 and 667 is: 100050

Example 2:




// Java code to show implementation of
// checkedMultiply(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 checkedMultiply(int a, int b) method
            // of Guava's IntMath class
            // This should throw "ArithmeticException"
            // as the product overflows in signed
            // int arithmetic
            int ans = IntMath.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[])
    {
        int a = Integer.MIN_VALUE;
        int b = 452;
  
        try {
  
            // Function calling
            findDiff(a, b);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output:

java.lang.ArithmeticException: overflow

Reference: https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/IntMath.html#checkedMultiply-int-int-

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32505 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6880 POSTS0 COMMENTS
Nicole Veronica
12003 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12097 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6966 POSTS0 COMMENTS
Umr Jansen
6954 POSTS0 COMMENTS