×

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

How to get ASCII value of char in Java

Introduction: On this application, you will learn how to find and show the ASCII value of char in Java. That is done with the use of type-casting and also everyday...

4 minutes read.

LCM Program in Java

LCM Program in Java The LCM program in Java outputs the LCM of the given numbers. In Arithmetic, the lowest common multiple, least common multiple or smallest common multiple of the...

6 minutes read.

Java Integer sum() method

The sum() method of Java Integer class add the two specified integers values. It returns the same result as given by + operator. Syntax public static int sum (int a, int b)  Parameters The...

1 minute read.

Insertion Sort in Java

Insertion Sort in Java Insertion sort in Java is a simple sorting algorithm that works in the same way as we hold cards in hand. Insertion sort does the sorting element-by-element,...

3 minutes read.

Java vs Golang

Java Java is both a programming language and a platform. Java is a high-level, dependable, object-oriented, and secure programming language. Java was developed in 1995 by Sun Microsystems, which is now an...

4 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

Binary Strings Without Consecutive Ones in Java

N, an integer, is provided. Our objective is to determine the overall number of strings with length N that do not include successive 1s. Example: Input: 3 Output: 6 Explanation The binary forms of length...

6 minutes read.

Heart Pattern in Java

Heart Pattern is yet another intricate pattern program, however, due to its complexity, interviewers hardly ever inquire about it. Method for Printing the Heart Number Pattern Put the value of the total row...

2 minutes read.

Equilibrium Index of an Array in Java

An array's equilibrium index is the condition where the sum of items with lower and higher indices equals. For example, an array A=[-4,5 2,6,-5] where: a[0]=-4, a[1]=5, a[2]=2, a[3]=6, a[4]=-5 Now according...

4 minutes read.

Zigzag Array in Java

In this tutorial, we discuss, what is zigzag array and its example. Even we will create the java program. In this program, we convert the simple array into a zigzag...

4 minutes read.

How to Convert Hexadecimal to Decimal in Java

How to Convert Hexadecimal to Decimal in Java There are two methods to convert Hexadecimal to Decimal: Using parseInt() method Using user-defined logic Using Integer.parseInt() method It is a static method of...

2 minutes read.

Java StringBuffer vs StringBuilder

Java StringBuffer vs StringBuilder StringBuffer The StringBuffer class is used to create mutable string. StringBuffer shows writable and growable character sequences. Java StringBuffer program FileName: StringBufferExample.java // A basic program that demonstrates the working...

2 minutes read.

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

Transaction Management in java

Definition: A database application is an application that is running against a relational database and executes one or more transactions. A transaction is an executing program that contains some database operations,...

4 minutes read.

Convert Char array to string in java

A collection of characters is referred to as a string. A character array differs from a string in that the string is canceled by the special character "\0." A string...

4 minutes read.

Accessors and Mutator in Java

Introduction Accessors and mutators are used in Java to get and set the value of private fields, respectively. Accessors and mutators are both referred to as getters and setters, respectively. The...

6 minutes read.

Lombok Java

What is Lombok java? A well-liked and widely-used Java framework that is used to reduce or eliminate boilerplate code is called Project Lombok. Both time and effort are saved. We may...

7 minutes read.

Java Logo

Java is a prominent and extensively used object-oriented programming language. In 1995, Sun Microsystems created it. Later in 2009, Oracle Corp takeover Java. History of Java Logo The name of the island...

3 minutes read.

Interfaces and Classes in Strings in Java

CharBuffer: CharBuffer is utilized to implement the CharSequence interface. With the help of the mentioned class, we can allow character buffers to be utilized instead of CharSequences. We can consider the illustration...

4 minutes read.

Array and String with Examples in Java

Array in Java: An array in Java is a group of variables with similar types that have a common name. The arrays used in Java differ from those used in C/C++. Key...

6 minutes read.