
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Multiply Exact in Java Math
In Java, multiplyExact() is an in-built static method of the Math class that accepts two arguments and returns their product as a result. This method can accept values of either integer or long type. One important point to note is that it may throw an exception if the produced result overflows the size of integer.
After knowing the functionality of multiplyExact() in Java, one question that may pop into one's mind is that we can also use '*' operator to multiply two operands then why do we need multiplyExact() method. We are going to answer this question and also, explore the multiplyExact() method with examples.
multiplyExact() method in Java
For a simple multiplication operation of two or more values of any type we can rely on the * operator, but we cannot multiply values of any type other than integer and long using multiplyExact() method. Also, it accepts only two arguments.
Before heading to the example programs, let's discuss the syntax of multiplyExact() method ?
Syntax
public static Type multiplyExact(Type val1, Type val2);
Here, the 'Type' specifies the integer or long datatype.
To use the multiplyExact() method in our Java program, we need to import the Math class using the following command ?
import java.lang.Math;
Note that multiplyExact() is a static method of Math class and to call this method we don't need an object rather we use the class name followed by the dot (.) operator.
Example
In the following example, we will use the multiplyExact() method to calculate the product of integer values.
import java.lang.Math; public class Example1 { public static void main(String args[]) { int a = 12, b = 34; System.out.printf("Product of a and b is: "); // performing the first multiplication System.out.println(Math.multiplyExact(a, b)); int c = 78, d = 93; System.out.printf("Product of c and d is: "); // performing the second multiplication System.out.println(Math.multiplyExact(c, d)); } }
Output
Product of a and b is: 408 Product of c and d is: 7254
Example
In this example, we will use the multiplyExact() method to calculate the product of long type values.
import java.lang.Math; public class Example2 { public static void main(String args[]) { long val1 = 1258, val2 = 304; System.out.printf("Product of val1 and val2 is: "); System.out.println(Math.multiplyExact(val1, val2)); long val3 = 478, val4 = 113; System.out.printf("Product of val3 and val4 is: "); System.out.println(Math.multiplyExact(val3, val4)); } }
Output
Product of val1 and val2 is: 382432 Product of val3 and val4 is: 54014
Example
The following example shows what will be the output if the result overflows the size of integer variable.
import java.lang.Math; public class Example3 { public static void main(String args[]) { int val1 = 1258526920; int val2 = 1304; System.out.printf("Product of val1 and val2 is: "); // this line will throw an exception System.out.println(Math.multiplyExact(val1, val2)); } }
Output
Product of val1 and val2 is: Exception in thread "main" java.lang.ArithmeticException: integer overflow at java.base/java.lang.Math.multiplyExact(Math.java:992) at Example3.main(Example3.java:7)
Example
We can handle the exception in a more professional way using try and catch block.
import java.lang.Math; public class Example4 { public static void main(String args[]) { try { int val1 = 1258526920; int val2 = 1304; System.out.println(Math.multiplyExact(val1, val2)); } catch(ArithmeticException exp) { // to handle the exception System.out.println("Calculation produced too big result"); } } }
Output
Calculation produced too big result
Conclusion
We started this article by introducing the multiplyExact() method which is an in-built static method of the Math class and in the next section, we discussed its practical implementation with the help of example programs. Also, we discovered how to handle the exception caused by the overflow of results.