Library contents

Kotlin java.lang.Math.multiplyFull

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

A simple example

java.lang.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.
Lesson.kt
fun main() {
    println(java.lang.Math.multiplyFull(2, 3))
}
Output
6

Syntax

Kotlin syntax
java.lang.Math.multiplyFull(rows, columns)
Run these examples in Kotlin

Save one example at a time as Lesson.kt. Use Kotlin 2.2 on JDK 21.

kotlinc Lesson.kt -include-runtime -d lesson.jar
java -jar lesson.jar

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 programval rows = 50000
Result2500000000
Lesson.kt

fun main() {
    val rows = 50000
    val columns = 50000
    println(java.lang.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 java.lang.Math.multiplyFull