Library contents

Java Math.multiplyFull

Multiply two int values and return the full product as a long.

A simple example

Math.multiplyFull

  1. Start with 2 and 3
  2. Do this Multiply
  3. Result 6
Two groups of three make six. This small product fits exactly.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Math.multiplyFull(2, 3));
    }
}
Output
6

Syntax

Java syntax
Math.multiplyFull(rows, columns)
JDK 21 method signatures (1)
public static long multiplyFull(int x, int y)
Run these examples in Java

Save one example at a time as Main.java. Use JDK 21.

java Main.java

Count cells in a large seating grid

A system must store a batch or grid count in a fixed-width field. Check that the calculation and the final conversion fit before committing the value to the record.

From the programint rows = 50000
Result2500000000
Main.java

class Main {
    public static void main(String[] args) throws Exception {
        int rows = 50000;
        int columns = 50000;
        System.out.println(Math.multiplyFull(rows, columns));
    }
}
Output
2500000000

A grid with 50000 rows and 50000 columns has 2.5 billion cells, beyond the positive int range.

Advertisements

Count postcards on a print sheet

A print shop lays out eight rows with six postcards in each row. It calculates how many finished cards a complete sheet produces.

Given8 · 6
Worked result48 items

Using 8 · 6 gives 48 items for this measurement.

Order bulbs for display panels

A gallery uses twelve panels that each take fifteen bulbs. The supply desk calculates the number of bulbs for a full installation.

Given12 · 15
Worked result180 items

Using 12 · 15 gives 180 items for this measurement.

What to watch for

Both operands must fit in int. For long operands choose a different overflow strategy.

Documentation for Math.multiplyFull