×

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

this keyword in Java

The 'this' keyword is an essential notion in Java. We'll go through the 'this' keyword in depth and provide some examples of how it's used in Java. In Java, the term...

5 minutes read.

Leap Year Program in Java

Leap Year Program in Java: A leap year is a calendar year that have an extra day i.e. 366 days instead of 365 days is called leap year. In other...

2 minutes read.

Packages in Java

Packages in Java can be defined as an assortment for grouping various classes and interfaces based on their performance. It is a catalog for holding various java files. They provide...

4 minutes read.

Java Viva Questions and Answers

Question: Explain Java programming language. Answer: It is a high-level programming language. This programming language is based on the principles of object-oriented programming. Large-scale applications can be developed by using this...

16 minutes read.

How to check Date Null in Java?

In this section, we will be acknowledged about Date Null in Java. The date null in Java is an entity that is used when there is no specified value for...

3 minutes read.

Java String endsWith() method

Checks whether this String ends with the specified suffix or not. Syntax public boolean endsWith(String Suffix) Parameter Suffix : It takes suffix as a parameter  i.e. Sequence of characters Returns It returns true when the current...

1 minute read.

How to write basic Java Programs

Java Basic Programs In this section, we will learn how to write basic Java programs. But first we need to take care of the following requirement list. To execute a Java program,...

4 minutes read.

Java Localization

Internationalization is the process of creating a software application that can be translated into different languages and regions without modifying the application. Creating a locale-specific application raises the cost of...

3 minutes read.

Java Integer parseInt() method

The parseInt() method of Java Integer class parses the CharSequence argument as a signed decimal integer. The second parameter parses the string argument as a signed integer in the radix specified...

2 minutes read.

Java String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 minutes read.

Java Integer longValue() method

The longValue()  method of Java Integer class returns a long value for this Integer after a widening primitive conversion. Syntax public long longValue() Parameters NA Specified by This method is specified by longValue in class Number Return...

1 minute read.

String to JSON in Java

Nowadays, receiving data in JSON String format rather than XML is quite frequent. Java does not transform JSON String to JSON Object when dealing with JSON String. However, using the...

3 minutes read.

How to handle NullPointerException in Java

Introduction: Throwable class is the super or root class of the java exception hierarchy which contains two sub-classes. The classes’ are- ExceptionError Exception: If you are doing any wrong things in a...

3 minutes read.

Java Date add Days

In order to operate with the time and the Date in Java, we used the abstract Calendar class. It has several helpful interfaces that enable us to convert dates between...

4 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.

Java import packages

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Short Circuit Logical Operators in Java

When there are two or more relational expressions in a decision-making statement, logical operators are utilized to combine them. The logical operators short circuit and not-short circuit fall into two...

5 minutes read.

Java Finally Keyword

The final block in Java is used to run essential code, such as connection closure, among other things. Whether an exception is resolved or not, the Java finally block has...

3 minutes read.

TreeSet in Java

Java TreeSet with Example Java TreeSet implements the Navigable Set interface. It stores the objects in ascending order. It contains unique elements. Access and retrieval time is fast. It does not...

8 minutes read.

HttpURLConnection

HttpURLConnection Protocol It is a standard set of rules that allow electronic devices to communicate with each other. The http protocol The http protocol is for data communication, distributing, collaborating, hypermedia information systems, on the World Wide...

5 minutes read.