Java Math.multiplyFull
Multiply two int values and return the full product as a long.
A simple example
Math.multiplyFull
- Start with 2 and 3
- Do this Multiply
- Result 6
class Main {
public static void main(String[] args) throws Exception {
System.out.println(Math.multiplyFull(2, 3));
}
}
6
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.javaCount 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.
class Main {
public static void main(String[] args) throws Exception {
int rows = 50000;
int columns = 50000;
System.out.println(Math.multiplyFull(rows, columns));
}
}
2500000000
A grid with 50000 rows and 50000 columns has 2.5 billion cells, beyond the positive int range.
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.
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.
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.