×

Java Math.multiplyExact() method in Java

Java has an inbuilt math function called Math.multiplyExact() that returns the sum of the parameters. If the result exceeds an integer, an exception is thrown. There is no need to create an object because multiplyExact(int n1, int n2) is static.

Syntax:

public static int multiplyExact(int n1, int n2)   
public static long multiplyExact(long n1, long n2)  
public static long multiplyExact(long n1, int n2)  

Parameters

It accepts two parameters:

n1=The first parameter

n2=The second parameter

Return

The Math.multiplyExact() method returns the Product of the arguments passed.

Exception

An ArithmeticException will be thrown if one of the parameters is Integer. MAX VALUE, Integer.MIN VALUE, Long.MAX VALUE, or Long.MIN VALUE

Example 1

Filename: MultiplyExactEx1.java

//Program for Math.multiplyExact() method in Java
//importing packages
import java.io.*;
import java.util.*;
public class MultiplyExactEx1  
{  
    public static void main(String[] args)   
    {  
        int n1 = 540;  
        int n2 = 2;  
        //The input value is integers 
        // Displays the Product of the two input values(n1*n2)
        System.out.println("The Product of two numbers is:"+Math.multiplyExact(n1,n2));  
    }  
}

Output

The Product of two numbers is:1080

Example 2

Filename: MultiplyExactEx2.java

//Program for Math.multiplyExact() method in Java
//importing packages
import java.io.*;
import java.util.*;
public class MultiplyExactEx2  
{  
    public static void main(String[] args)   
    {  
        long n1 = Long.MAX_VALUE;  
        long n2 = 92;  
        // the input value n1 is overflow
        //the output returns the exception
        System.out.println(Math.multiplyExact(n1, n2));  
    }  
}  

Output

Exception in thread "main" java.lang.ArithmeticException: long overflow
	at java.base/java.lang.Math.multiplyExact(Math.java:949)
	at MultiplyExactEx2.main(MultiplyExactEx2.java:13)

Example 3

Filename: MultiplyExactEx3.java

//Program for Math.multiplyExact() method in Java
//importing packages
import java.io.*;
import java.util.*;
public class MultiplyExactEx3  
{  
    public static void main(String[] args)   
    {  
        int n1 = Integer.MIN_VALUE;  
        int n2= 303;  
        // raises an error   
        System.out.println(Math.multiplyExact(n1, n2));  
    }  
}

Output

Exception in thread "main" java.lang.ArithmeticException: integer overflow
	at java.base/java.lang.Math.multiplyExact(Math.java:909)
	at MultiplyExactEx3.main(MultiplyExactEx3.java:12)

Related Topics

Palindrome number program in Java

Write java program to check if a number is palindrome in Java? A number that does not change on reversing is called a palindrome number. In other words, when reversing the...

3 minutes read.

Program to check whether a given character is present in a string or not

In this article, you will understand the logic to find out whether the given character is present in the string or not and find out the position of the specified...

3 minutes read.

Stone Game in Java

In this tutorial, we will learn to design a stone game in Java. First of all, we will understand what is this game all about. We will grasp it through...

9 minutes read.

Java Primitive Data Types

Primitive data types are the simplest data types in a programming language. They’re predefined in the language. The names of the primitive types are quite descriptive of the values that...

2 minutes read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.

Java Interface Keyword

An interface is also known as the blueprint in Java. It has constants of static values and methods of abstraction. The interface is a mechanism used by Java to declare...

3 minutes read.

This Operator Using in Java

this keyword in Java has a wide range of applications. this reference variable in Programming language refers to the object of interest. This keyword is used in Java. this keyword is...

5 minutes read.

How annotations work in Java

Annotations were introduced by sun microsystem and are available from the java 1.5 version. In general, configurations can be done either by XML or by annotations. Hibernate and spring framework users prefer...

4 minutes read.

Java Custom Exception

Java Custom Exception Java language facilitates us to create our own exceptions. The class intended to throw the Custom Exception has to be derived from the Java Exceptionor RuntimeExceptionclass. The Java...

4 minutes read.

Java.net.ConnectionException

java.net.ConnectException: Connection rejected: the interface is the most continuous sort of happening, organizing special cases in Java at whatever point the product is in client-server engineering and attempting to make...

3 minutes read.

Java logger

Logging is a crucial Java feature that aids developers in identifying issues. The logging methodology is included with Java as the programming language. It offers the Logging API, which debuted...

7 minutes read.

Thread Scheduler in java

Scheduling: it is defined as the execution of multiple threads on a single CPU in some order is called scheduling. Preemptive-priority scheduling: This algorithm schedules threads based on their priority relative to other...

11 minutes read.

Pancake Sorting in Java

In the pancake sorting method, the array must be sorted using just one operation, which is: Flip the arrays arr at index 0 to index j by using flipArr(arr, h). Other sorting...

3 minutes read.

Sales Tax Problem in Java

To calculate the sales tax on a purchase in Java, you will need to know the following information: The cost of the item being purchased The sales tax rate You can then use...

4 minutes read.

Get yesterdays date by no of days in Java

In this tutorial, we are going to learn how to get yesterday’s date by the no of days in Java. Using the Calendar class, one can get the current date....

1 minute read.

Set Up the Environment in Java

The Java is regarded as the pure object-oriented programming language. The Java applications are first compiled into the byte-code and then run by using the JVM. The Java is the...

4 minutes read.

Java Integer rotateLeft() method

The rotateLeft() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value left by the specified number of bits. Syntax public...

1 minute read.

How to Send SMS in Java with Example

Sending SMS messages in Java is a fairly common task, and there are a number of libraries and APIs available to help you do it. One popular option is to...

2 minutes read.

Convert IP to Binary in Java

Fundamental conversion, such as going from binary to decimal or vice versa, is a crucial activity in computers. Understanding IP addressing and subnetting is crucial for networking. The primary networking...

4 minutes read.