×

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

Enum Java Interview Questions

1. What exactly is Java? Java is a portable, high-level, object-oriented, robust, secure, platform-independent, multi-threaded, high-performance programming language. Developed in June 1991 by James Gosling, also known as Platform. 2. Enum is...

3 minutes read.

Java Int Keyword

Among the primitive data types is the Java int keyword. To declare variables, use this. It can also be used with methods that return values of the integer type. It...

3 minutes read.

Hollow Diamond Pattern in Java

Why are patterns important? Programmers frequently create Java pattern programs to practice coding and ace interviews. Interviewers frequently test candidates' logical reasoning and implementation by asking about pattern programs. Hollow Diamond Pattern The...

7 minutes read.

How to Convert char to int in Java

How to Convert char to int in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

3 minutes read.

Java Try-Catch Block

Java Try Block The handling of the exceptions in a block of code is done with the help of java try block. It throws the code that is enclosed in a...

3 minutes read.

How to Convert Integer to String in Java

How to Convert int to String in Java It is used when you want to convert an integer to String. You can convert int to String by using the following methods: Using...

3 minutes read.

Java String getBytes() method

getBytes() method returns sequence of bytes. Syntax: There are three variants of getBytes() method: public byte[] getBytes()        public byte[] getBytes(String charsetName) public byte[] getBytes(Charset charset) Returns: It returns sequence of bytes. Java String getBytes() Example...

2 minutes read.

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced...

5 minutes read.

Difference between JDK, JRE and JVM In Java

In the Java ecosystem, three primary components are often mentioned: JDK, JRE, and JVM. Here’s a breakdown of each: Java Development Kit (JDK) The Java Development Kit (JDK) is a comprehensive software...

2 minutes read.

Java Math toRadians() Method

The toRadians() method of Java Math class converts an angle measured in degrees to an approximately equivalent angle measured in radian. Syntax: public static double toRadians (double angdeg) Parameters The parameter ‘angdeg’ represents an...

2 minutes read.

Difference between String and Char Array in Java

We are heading to examine some significant differences between String and Character arrays. Both char arrays and String hold the series of characters and are utilised as a cluster of...

3 minutes read.

Java While Loop

A while loop is used to repeatedly execute a set of statements as long as its condition evaluates to true. This loop checks the condition before it starts the execution...

1 minute read.

Java Math addExact() Method

The addExact() method of Math class returns the sum of the two arguments, throwing an exception if the result overflows a long or an int. Syntax public static int addExact (int x,...

1 minute read.

How to Read CSV Files in Java?

Comma-Separated Values is the acronym for this format. It is a straightforward file format storing textual tabular data in a database or spreadsheet. The CSV files can be imported into...

3 minutes read.

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.

Different Ways to Take Input from User in Java

Any information provided to a system for use is known as user input. Any responsive software or application must include user input. In Java, there are 4 different ways to...

5 minutes read.

Java Boolean logicalXor() Method

The logicalXor() method of Java Boolean class returns the result of implementing logical XOR operation on the specified Boolean operands. Syntax: public static boolean logicalXor (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

Converting Roman to Integer Numerals in java

In this article, you will be acknowledged about the process of conversion of roman numbers to integers in java. The most important approaches are discussed and also the implementation of...

3 minutes read.

Java Enumeration

In a computer language, enumerations express a set of named constants. For instance, the four suits in a deck of playing cards could be represented by the enumerators Club, Diamond,...

4 minutes read.

String Concatenation in Java

In Java, it gathers a new String that combines several strings. Following are the manners to concatenate strings in Java: By + (String concatenation) operatorBy concat() method By + (String concatenation) operator Java...

4 minutes read.