×

Java Math.multiplyExact() method

In java, we are provided with multiplyExact method in the Math module. This Math module belongs to the java.lang package. In general, this method returns the product of the provided arguments. As this method takes in integer values and also returns the product which is an integer, this method throws an exception if the product value exceeds the range of int datatype. multiplyExact method is declared as static, so object creation is not necessary to implement this method.

SYNTAX:

This is the syntax of the multiplyExact method when integers are taken as the parameters.

public static int multiplyExact (int x, int y)

This is the syntax of the multiplyExact method when double values are taken as the parameters.

public static double multiplyExact (double x, double y)

Parameters: x,y are the parameters. These can be of integer or double datatypes. 

This method returns an integer value if the parameters provided are integer values. In the same way, it returns a double value if the corresponding parameters are double values.

NOTE:

This method throws an ArithmeticException, if the produced result overflows with the range of int datatype.

We should import the java.lang package in the java program.

Let’s look the implementation of the multiplyExact() Method in java

import java.io.*; 
import java.lang.Math;


class MultiplyExactDemo
{
public static void main (String args []) 
{
System.out.println (“The implementation of the multiplyExact method by taking integer parameters”);
int p = 23, q = 10;
System.out.println (“The product of two integer values is” +Math.multiplyExact(p, q));
System.out.println (“The implementation of the multiplyExact method by taking integer parameters”);
long r =10, s = 5;
System.out.println (“The product of two long values is” +Math.multiplyExact(r, s));
} //main
} //MultiplyExactDemo

OUTPUT:

The implementation of the multiplyExact method by taking integer parameters
The product of two integer values is 230
The implementation of the multiplyExact method by taking integer parameters
The product of two long values is 50

Let’s look into another example for implementing the multiplyExact Method which throws an exception.

import java.io.*;
import java.lang.Math;


class MultiplyExactDemo
{
public static void main (String args []) 
{
System.out.println (“The implementation of the multiplyExact method by taking integer parameters”);
int p = Integer.MAX_VALUE; 
 int q = 10;
System.out.println (“The product of two integer values is” +Math.multiplyExact(p, q));
} //main
} //MultiplyExactDemo

OUTPUT:

Runtime Error:
Exception in thread “main” java. lang. ArithmeticException: integer overflow 
at java. lang. Math. multiplyExact (Math. java: 867)
at MultiplyExactDemo.main(File. java: 13)

Related Topics

Difference Between Java and PHP

The two most used programming languages are PHP and Java. Both of them have a lot of similarities and distinctions. Let's first grasp each of them individually before examining their...

3 minutes read.

Race Condition in Java

Java is a multi-threaded programming language, race conditions are more likely to arise. Mostly because data can change when multiple threads visit the same resource simultaneously. Race conditions are concurrency...

3 minutes read.

Java Math log10() Method

The log10() method of Math class returns the base 10 logarithmic value for the specified double argument. Syntax: public static double log10(double a) Parameters: The parameter ‘a’ represents a double value. Return Value: The log10() method...

2 minutes read.

Prime Number Program in Java

Prime Number Program in Java using for loop A natural number which is greater than 1 and has only two factors the number itself and 1 is called prime number. In...

2 minutes read.

Math fma () method in java

In java, the Math module constitutes of fma () Method in it. This method can be accessed in two ways which can be differentiated by the parameters which are given...

4 minutes read.

Chromatic Number in Java

The chromatic number is the bare minimum of colours necessary to accurately colour any graph. To put it another way, the chromatic number can be thought of as the bare...

6 minutes read.

Snake and Ladder Problem in Java

Find the smallest number of dice throws necessary to reach the destination or last cell from the source or first cell on a snake and ladder board. Essentially, the player...

6 minutes read.

Java Continue Keyword

The java keyword ‘continues’ has also been called as java continue statement.The main concepts of the java continue statement or keyword is used in the controlling of the loop structure.Java...

3 minutes read.

How to Convert String to Date in Java

How to Convert String to Date in java You can convert String to Date in Java by using the parse() method. There are two classes which have parse() method namely, DateFormat and SimpleDateFormat classes....

2 minutes read.

Determine the Upper Bound of a Two-Dimensional Array in Java

Multi-Dimensional Array Multi-faceted exhibits in Java are regular and can be named various clusters. Information in a two-layered bunch in Java is put away in 2D even structure. A two-layered collection...

3 minutes read.

Java Boolean toString() Method

The toString() method of Java Boolean class returns a String corresponding to this Boolean object. It returns a string value “true”, if the defined object is true else it returns...

2 minutes read.

Java program to determine whether all leaves are at same level

In this program, we must determine whether or not all of the binary tree's leaves are at the same level. If a node has no child nodes, it is said to...

3 minutes read.

Reserved Keywords in Java

In Java, a reserved term is used as a code key called a keyword. Because they are predefined, these terms cannot be used for anything else. They cannot serve as...

3 minutes read.

Object Definition in Java

In this article, you will be acknowledged about object in Java, its definition and how is this useful in writing the programs in Java. The comprehension of object-oriented technology depends on...

4 minutes read.

How to Reduce Time Complexity in Java

What is time complexity?  The time complexity in java is given as the amount of time a program requires to run or execute it Calculating the time complexity of the program The time...

4 minutes read.

Java Integer min() method

The min()  method of Integer class returns the smaller of two int values. It returns the same result as by calling Math.min.  Syntax public static int min(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

Java Stringjoiner Class

StringJoiner is a class which is used to construct a sequence of characters which are separated by a delimiter. Optionally, it starts with a provided prefix and ended with the...

5 minutes read.

Computing Digit Sum of all Numbers from 1 to n in Java

In this tutorial, we will discuss various methods to compute the sum of digits of all numbers beginning from 1 to n through a Java program. We will achieve it through...

7 minutes read.

AbstractSet Class in Java

The AbstractSet class is used for the implementation of the Abstract Collection class and interface. It is the part of Collection Frameworks. In the AbstractSet, the implementation is same as the...

3 minutes read.

How to add 4 Hours to the Current Date in Java?

In this tutorial, we will learn how to add 4 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.