java.lang.Math.multiplyExact()

Description

On this document we will be showing a java example on how to use the multiplyExact() method of Math Class. The multiplyExact() returns the product of the arguments, throwing an exception if the result overflows the specified datatype either long or int depending on which data type has been used on the method argument. Make a note that the multiplyExact() method is overloaded. Below are the two overloaded method of the multiplyExact() method:

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

The two overloaded methods are basically the same, it’s just that they deal with different data type either int or long.

Most of the methods of the Math class is static and the multiplyExact() method is no exception. Thus don’t forget that in order to call this method, you don’t have to create a new object. Use the method in the format Math.multiplyExact(x,y).

From the first part we have described this method to throw an exception if the result overflows the specified data type. If you are interested on the limit of the result before it throws an exception, you can find it using Integer.MIN_VALUE and Integer.MAX_VALUE if you are interested on int datatype. Meanwhile if you are interested on the range for long, you can find it using Long.MAX_VALUE and Long.MIN_VALUE.

With the complicated description above on this method functionality, you might start to wonder why not use directly multiplication. That might be the easiest solution though if we breach the limitation of data type the result would be inconsistent, not unlike if we use the multiplyExact() which has the capability to throw an exception if the result overflows of its range. So with this capability to throws an exception, will give us room to provide mechanism to handle such scenario.

Notes:

  • ArithmeticException will be throw if the operation overflows the specified data type.

Method Syntax

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

Method Returns

The multiplyExact() method returns the product of the arguments.

Compatibility

Requires Java 1.8 and up

Java Math multiplyExact() Example

Below is a java code demonstrates the use of multiplyExact() method of Math class. The example presented might be simple however it shows the behavior of the multiplyExact() method.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of  
 * multiplyExact() method of Math class
 */

public class MathMultiplyExactExample {

	public static void main(String[] args) {

		// Ask for user input
		System.out.print("Enter 1st value:");

		// use scanner to read the console input
		Scanner scan = new Scanner(System.in);

		// Assign the user to String variable
		String value1 = scan.nextLine();
		
		
		// Ask for user input
		System.out.print("Enter 2nd value:");

		// Assign the user to String variable
		String value2 = scan.nextLine();

		// close the scanner object
		scan.close();

		// convert the string input to int
		int x = Integer.parseInt(value1);
		int y = Integer.parseInt(value2);

		// get the result of multiplyExact()
		int result = Math.multiplyExact(x,y);
		System.out.println("Result of the operation is " + result);

	}

}

The above java example source code demonstrates the use of multiplyExact() method of Math class. We simply ask for a 2 user input and we use the Scanner class to parse it. Since we have used the nextLine() method to get the console value which is having a return data type of String thus we have used the Integer.parseInt  to transform it into int. Alternatively if the requirements is to use long then you must use the Long.ParseLong() instead. This conversion is required because the argument for multiplyExact() method only accepts either int or long.

Sample Output

Below is the sample output when you run the above example.

java lang Math multiplyExact() example output