Java Math multiplyFull() Method

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

Definition

In Java, Math.multiplyFull(x, y) multiplies two int values and returns the exact product as a long. The product of two ints always fits in a long, so this method never overflows and never throws.

It does the same job as the well-known idiom (long) x * y, but the intent is written in the method name, and nobody can forget the cast or put it in the wrong place.

A static method of the java.lang.Math class, added in Java 9 (JDK 9). Call it on the class name, as in Math.multiplyFull(...). No import is needed, because classes in java.lang are always visible. Requires Java 9 or newer.

Syntax

Pass two int values. The result is a long, so store it in a long variable.

Java
long area = Math.multiplyFull(50_000, 60_000);       // 3000000000
long same = (long) 50_000 * 60_000;                  // 3000000000
int wrong = 50_000 * 60_000;                         // -1294967296 (overflow)
CallWhat it does
Math.multiplyFull(50_000, 60_000)Returns the long 3000000000.
Math.multiplyFull(-3, 4)Returns the long -12.
Math.multiplyFull(Integer.MIN_VALUE, Integer.MIN_VALUE)Returns 4611686018427387904, the largest possible int product.
Math.multiplyFull(w, h) * bytesPerPixelKeep working in long for the rest of the calculation.

Only int arguments are accepted. For two long values the product can need up to 128 bits, so Java offers multiplyExact (throws on overflow) and multiplyHigh (the upper half) instead.

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

Parameters

ParameterDescription
xThe first int factor.
yThe second int factor.

Return Value

Return ValueDescription
longThe exact product x * y. Never overflows, because 32 bits times 32 bits always fits in 64 bits.

Compare with Similar Methods

Different ways to multiply two ints:

MethodHow it differs
x * yResult is an int; overflows silently.
(long) x * ySame result as multiplyFull; works on every Java version.
multiplyExact(x, y)Keeps the int type but throws if the product does not fit.
multiplyHigh(x, y)For two long factors: the upper 64 bits of the product. Java 9+.

Java Examples

Example 1 — The Same Two Ints, Two Different Answers

Multiply large int values the plain way and with multiplyFull. This example needs Java 9 or newer.

Java
public class MathMultiplyFullCompare {
    public static void main(String[] args) {
        int a = 100_000;
        int b = 100_000;
        System.out.println("plain int:  " + (a * b));
        System.out.println("full:       " + Math.multiplyFull(a, b));
        System.out.println("MAX * MAX:  " + Math.multiplyFull(Integer.MAX_VALUE, Integer.MAX_VALUE));
        System.out.println("MIN * MIN:  " + Math.multiplyFull(Integer.MIN_VALUE, Integer.MIN_VALUE));
    }
}

How It Works

  1. Plain int wraps. Both factors are int, so a * b is computed in 32 bits; the true 10000000000 is cut to 1410065408.
  2. Full width. multiplyFull does the multiplication in 64 bits, so the same two ints give the correct 10000000000.
  3. The biggest cases. 2147483647 squared is 4611686014132420609, and (-2147483648) squared is 4611686018427387904 (exactly 2 to the power 62); both are far below the long limit, which is why multiplyFull can never overflow.

Example 2 — How Many Pixels Does a Huge Image Have?

A 50,000 by 60,000 pixel image has more pixels than an int can count. Java 9+.

Java
public class MathMultiplyFullImage {
    public static void main(String[] args) {
        int width = 50_000;
        int height = 60_000;

        System.out.println("plain int area:  " + (width * height));
        long pixels = Math.multiplyFull(width, height);
        System.out.println("pixels (long):   " + pixels);
        System.out.println("fits in an int?  " + (pixels <= Integer.MAX_VALUE));
        System.out.println("bytes at 3 each: " + pixels * 3);
    }
}

How It Works

  1. A negative pixel count. 50000 times 60000 is 3000000000, above 2147483647, so the plain int product wraps to -1294967296, which is impossible for a count.
  2. Long keeps the truth. multiplyFull returns 3000000000, and the comparison with Integer.MAX_VALUE confirms it no longer fits in an int.
  3. Stay in long afterwards. pixels * 3 is a long times an int, so the byte count 9000000000 is also computed without overflow.

Example 3 — Add Up Products in a long Accumulator

A dot product multiplies pairs and sums them. Compare a long accumulator fed by multiplyFull with a plain int sum. Java 9+.

Java
public class MathMultiplyFullDot {
    public static void main(String[] args) {
        int[] a = {60_000, 70_000, 80_000};
        int[] b = {50_000, 40_000, 30_000};
        long dot = 0;
        int wrong = 0;
        for (int k = 0; k < a.length; k++) {
            dot += Math.multiplyFull(a[k], b[k]);
            wrong += a[k] * b[k];
        }
        System.out.println("Dot product: " + dot);
        System.out.println("Plain int:   " + wrong);
    }
}

How It Works

  1. Three big products. The pairs give 3000000000, 2800000000, and 2400000000, and each one is already larger than the biggest int.
  2. Accumulate in long. dot is a long, so the exact products add up to the correct 8200000000.
  3. The int loop goes wrong twice. Each product wraps (to -1294967296, -1494967296, and -1894967296), and their sum wraps again, ending at -389934592.

Frequently Asked Questions

It multiplies two int values and returns the exact product as a long. Because the product of two ints always fits in a long, it never overflows or throws.
Java 9. On Java 8 and older use (long) x * y, with the cast on one of the operands.
multiplyFull multiplies in 64 bits. In (long) (x * y) the multiplication is done as int first and may already have overflowed, so the cast only widens a wrong answer.
No. The largest possible product of two ints, Integer.MIN_VALUE times Integer.MIN_VALUE, is 4611686018427387904, which is far below Long.MAX_VALUE.
No. Two longs can produce up to 128 bits, so use Math.multiplyExact to get an exception on overflow, or Math.multiplyHigh to get the upper 64 bits.

Next: multiplyHigh()

The upper 64 bits of the 128-bit product of two longs (Java 9+).

multiplyHigh() 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.

15 people found this page helpful