Facebook Page Twitter Page LinkedIn Page
× Java Tutorial Java Advanced Java Resources


The Java Math multiplyExact() method is used to return product of its arguments. The method can be overloaded and it can take int and long arguments. It throws an exception, if the result overflows an int or a long.

Syntax

public static int multiplyExact(int x, int y)  
public static long multiplyExact(long x, long y)  

Parameters

x Specify the first value.
y Specify the second value.

Return Value

Returns product of its arguments.

Exception

throws ArithmeticException, if the result overflows an int or a long.

Example:

In the below example, multiplyExact() method is used to multiply given numbers.

public class MyClass {
 public static void main(String[] args) {
  int x = 12, y = 17;
  long p = 145, q = 139;
  System.out.println(Math.multiplyExact(x, y)); 
  System.out.println(Math.multiplyExact(p, q));    
 }
}

The output of the above code will be:

204
20155

❮ Java Math Methods