The java.lang.Math.multiplyExact() is a built-in math function in java which returns the product of the
arguments.It throws an exception if the result overflows an int.As multiplyExact(int a, int b) is static, so
object creation is not required.
Syntax :
public static int multiplyExact(int a, int b) public static double multiplyExact(int a, double b) Parameter : a : the first value b : the second value Return : This method returns the product of the arguments. Exception : It throws ArithmeticException - if the result overflows an int
Example :To show working of java.lang.Math.multiplyExact() method.
// Java program to demonstrate working // of java.lang.Math.multiplyExact() method import java.lang.Math; class Gfg1 { // driver code public static void main(String args[]) { int a = 25, b = 5; System.out.println(Math.multiplyExact(a, b)); long c = 100, d = 50; System.out.println(Math.multiplyExact(c, d)); } } |
125 5000
// Java program to demonstrate working // of java.lang.Math.multiplyExact() method import java.lang.Math; class Gfg2 { // driver code public static void main(String args[]) { int x = Integer.MAX_VALUE; int y = 2; System.out.println(Math.multiplyExact(x, y)); } } |
Output:
Runtime Error :
Exception in thread "main" java.lang.ArithmeticException: integer overflow
at java.lang.Math.multiplyExact(Math.java:867)
at Gfg2.main(File.java:13)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- StrictMath multiplyExact() Method in Java
- Java.math.BigInteger.probablePrime() method in Java
- Java.math.BigInteger.modInverse() method in Java
- Java.lang.Math Class in Java | Set 2
- java.lang.Math.atan2() in Java
- Java.lang.Math Class in Java | Set 1
- Java Math exp() method with Example
- Java Math log() method with example
- Math pow() method in Java with Example
- Java Math subtractExact(int a , int b) method
- Java Math addExact(int a, int b) method
- Java Math incrementExact(int x) method
- Java Math min() method with Examples
- Math fma() method in Java with Examples
- Java Math hypot() method with Example
- Math floorDiv() method in Java
- Java Math sin() method with Examples
- Java Math round() method with Example
- Java Math expm1() method with Example
- Java Math getExponent() method with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

