Java Math multiplyExact() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

In Java, Math.multiplyExact(x, y) returns the product x * y, but throws an ArithmeticException if the product does not fit in the type. The plain * operator keeps only the low bits and quietly returns a wrong number.

Multiplication overflows much sooner than addition: 100000 times 100000 already does not fit in an int. That makes multiplyExact useful for factorials, areas, unit conversions, and any price times quantity calculation.

A static method of the java.lang.Math class, added in Java 8 (JDK 1.8). Call it on the class name, as in Math.multiplyExact(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass two int values or two long values. A long and an int together use the long overload.

Java
int area = Math.multiplyExact(120, 30);        // 3600
long ms = Math.multiplyExact(86_400L, 1000);      // 86400000
Math.multiplyExact(100_000, 100_000);             // throws ArithmeticException
CallWhat it does
Math.multiplyExact(12, 30)int overload: returns 360.
Math.multiplyExact(100_000L, 100_000L)long overload: returns 10000000000L.
Math.multiplyExact(factorial, n)A long times an int: the running product of a factorial loop.
Math.multiplyExact(100_000, 100_000)Throws: 10000000000 does not fit in an int.

Java 9 added a dedicated multiplyExact(long x, int y) overload. On Java 8 the call still compiles, because the int is widened to long and multiplyExact(long, long) is used; the result is the same.

Library declarations
Java
// java.lang.Math
public static int multiplyExact(int x, int y)
public static long multiplyExact(long x, int y)
public static long multiplyExact(long x, long y)

Parameters

ParameterDescription
xThe first factor.
yThe second factor.

Return Value

Return ValueDescription
int / longThe product x * y, when it fits in the result type.
Throws ArithmeticExceptionWhen the product does not fit: integer overflow for the int version, long overflow for the long versions.

Compare with Similar Methods

Ways to multiply whole numbers in Java:

MethodHow it differs
x * yNo check: 100_000 * 100_000 silently becomes 1410065408.
multiplyFull(x, y)Two ints in, the exact product out as a long; never throws. Java 9+.
multiplyHigh(x, y)The upper 64 bits of a 128-bit product of two longs. Java 9+.
addExact(x, y)The matching overflow guard for addition.

Java Examples

Example 1 — Products That Fit Match the * Operator

Ordinary multiplications with an int, a negative factor, and two long overloads.

Java
public class MathMultiplyExactBasics {
    public static void main(String[] args) {
        System.out.println(Math.multiplyExact(12, 30));
        System.out.println(Math.multiplyExact(-7, 6));
        System.out.println(Math.multiplyExact(100_000L, 100_000L));
        System.out.println(Math.multiplyExact(5_000_000_000L, 3));
    }
}

How It Works

  1. Positive and negative. 12 times 30 is 360 and -7 times 6 is -42; both products are tiny compared with the int limit.
  2. A product beyond int. 100000 times 100000 is 10000000000, which needs a long. The L suffixes select the long overload, so nothing overflows.
  3. Long times int. In multiplyExact(5_000_000_000L, 3) the second argument is an int; Java 9 and newer use the (long, int) overload, Java 8 widens the 3 to long, and both return 15000000000.

Example 2 — The Wrong Answer vs the Exception

Multiply two numbers whose true product is too large and compare what each approach does.

Java
public class MathMultiplyExactOverflow {
    public static void main(String[] args) {
        int a = 100_000;
        int b = 100_000;
        System.out.println("plain int *:  " + (a * b));
        try {
            System.out.println("multiplyExact: " + Math.multiplyExact(a, b));
        } catch (ArithmeticException e) {
            System.out.println("int threw:    " + e.getMessage());
        }
        System.out.println("plain long *: " + (Long.MAX_VALUE * 2));
        try {
            Math.multiplyExact(Long.MAX_VALUE, 2L);
        } catch (ArithmeticException e) {
            System.out.println("long threw:   " + e.getMessage());
        }
    }
}

How It Works

  1. Only the low 32 bits survive. The true product is 10000000000. Java keeps just the low 32 bits, and those happen to read as 1410065408, a plausible-looking but wrong number.
  2. No result line for the exact call. multiplyExact throws before println can print, so only the catch block's line integer overflow appears.
  3. Long wraps too. Long.MAX_VALUE * 2 is 18446744073709551614, which wraps down to -2; the exact version reports long overflow instead.

Example 3 — Find Where a Factorial Stops Fitting in a long

Multiply 1 * 2 * 3 * ... until the long runs out of room, then compare with a plain int loop.

Java
public class MathMultiplyExactFactorial {
    public static void main(String[] args) {
        long factorial = 1;
        for (int n = 1; n <= 25; n++) {
            try {
                factorial = Math.multiplyExact(factorial, n);
            } catch (ArithmeticException e) {
                System.out.println("Last safe: " + (n - 1) + "! = " + factorial);
                System.out.println(n + "! overflows: " + e.getMessage());
                break;
            }
        }

        int wrong = 1;
        for (int n = 1; n <= 13; n++) {
            wrong *= n;
        }
        System.out.println("13! with plain int: " + wrong);
    }
}

How It Works

  1. Growing product. Each pass multiplies the running long by the next n; 20! is 2432902008176640000, just under Long.MAX_VALUE (about 9.22 quintillion).
  2. The first failure is reported. 21! would be 51090942171709440000, more than five times the limit, so multiplyExact throws, and the message plus n - 1 tell you the last safe value.
  3. The silent int version. 13! is really 6227020800, but the plain int loop wraps it to 1932053504 and prints no warning at all.

Frequently Asked Questions

It multiplies two ints or longs and returns the product. If the product does not fit in the result type, it throws ArithmeticException instead of returning a wrapped-around number.
Use Math.multiplyExact and catch ArithmeticException, or widen to long with Math.multiplyFull(int, int) (Java 9+) and compare with the int range yourself.
An overload added in Java 9 that multiplies a long by an int and returns a long. On Java 8 the int is simply widened to long and the (long, long) version is used.
"integer overflow" for the int version and "long overflow" for the long versions, both inside an ArithmeticException.
The true product, 10000000000, needs more than 32 bits. Java keeps only the low 32 bits of the result, which read as 1410065408. Use multiplyExact or a long to avoid it.

Next: multiplyFull()

Multiply two ints and get the exact result as a long (Java 9+).

multiplyFull() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful