Example
Multiply integers using multiplyExact():
java
System.out.println(Math.multiplyExact(96000, 1200));
System.out.println(Math.multiplyExact(-460, 95));
Definition and Usage
The multiplyExact() method multiplies two integers and throws an exception if the result causes an overflow. This prevents incorrect results that can occur from multiplying really large numbers.
Syntax
One of the following:
java
public static int multiplyExact(int x, int y)
java
public static long multiplyExact(long x, long y)
Parameter Values
| Parameter | Description |
|---|---|
| x | Required. The first number to be multiplied. |
| y | Required. The second number to be multiplied. |
Technical Details
| Returns: | An int or long value representing the multiplication of two numbers. |
|---|---|
| Throws: | ArithmeticException - If the multiplication causes an overflow. |
| Java version: | 1.8+ |