Java Math multiplyExact() Method
Example
Multiply integers using multiplyExact()
:
System.out.println(Math.multiplyExact(96000, 1200));
System.out.println(Math.multiplyExact(-460, 95));
Try it Yourself »
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:
public static int multiplyExact(int x, int y)
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+ |
❮ Math Methods