Java.lang Package Classes

Java Math - multiplyExact() Method



The java.lang.Math.multiplyExact() method returns product of its arguments. It throws an exception, if the result overflows a long.

Syntax

 
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 a long.

Example:

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

import java.lang.*;

public class MyClass {
 public static void main(String[] args) {
  long x = 12;
  long y = 17;
  long p = 145;
  long 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.lang - Math

5