StrictMath multiplyExact() Method in Java
- The multiplyExact(int num1, int num2) is an inbuilt function of StrictMath class in java which is used to get the product of the given arguments. When the result overflows an int i.e. Integer.MAX_VALUE then it throws an exception. Since this function is static so there is no need to create objects.
Syntax :public static int multiplyExact(int num1, int num2)
Parameters : The method accepts two parameters :
- num1: This is of integer type representing one parameter
- num2: This is of integer type representing another parameter
Return Value : The method returns the product of num1 and num2.
Exception : It throws ArithmeticException when the result overflowsan int.
Examples :
Input: num1 = 8 nm2 = 7 Output: 56
Below program illustrate the Java.lang.StrictMath.multiplyExact() method.
// Java praogram to illustrate the// Java.lang.StrictMath.multiplyExact()importjava.lang.*;classGeeks {publicstaticvoidmain(String args[]){intnum1 =10, num2 =5, num3 = -17, num4 = -2;System.out.println("Product of "+ num1 +" and "+ num2 +" is "+ StrictMath.multiplyExact(num1, num2));System.out.println("Product of "+ num3 +" and "+ num4 +" is "+ StrictMath.multiplyExact(num3, num4));System.out.println("Product of "+ num2 +" and "+ num4 +" is "+ StrictMath.multiplyExact(num2, num4));}}chevron_rightfilter_noneOutput:Product of 10 and 5 is 50 Product of -17 and -2 is 34 Product of 5 and -2 is -10
- The multiplyExact(long num1, int num2) is an inbuilt function of StrictMath class in java which is used to get the product of the given arguments. When the result overflows a long then it throws an exception. Since this function is static so there is no need to create objects.
Syntax :public static long multiplyExact(long num1, int num2)
Parameters: The method accepts a two parameters :
- num1: This is of long type representing one parameter
- num2: This is of int type representing another parameter
Return Value : The method returns the product of num1 and num2.
Exception : It throws ArithmeticException when the result overflows an int .
Examples :Input: num1 = 8727 nm2 = 2 Output: 17454
Below program illustrate the Java.lang.StrictMath.multiplyExact() method.
// Java praogram to illustrate the// Java.lang.StrictMath.multiplyExact()importjava.lang.*;classGeeks {publicstaticvoidmain(String args[]){longnum1 =2727810, num2 =9892829;intnum3 =817, num4 =3;System.out.println("Product of "+ num1 +" and "+ num2 +" is "+ StrictMath.multiplyExact(num1, num2));System.out.println("Product of "+ num3 +" and "+ num4 +" is "+ StrictMath.multiplyExact(num3, num4));System.out.println("Product of "+ num2 +" and "+ num4 +" is "+ StrictMath.multiplyExact(num2, num4));}}chevron_rightfilter_noneOutput:Product of 2727810 and 9892829 is 26985757874490 Product of 817 and 3 is 2451 Product of 9892829 and 3 is 29678487
- The multiplyExact(long num1, long num2) is an inbuilt function of StrictMath class in java which is used to get the product of the given arguments. When the result overflows a long then it throws an exception. Since this function is static so there is no need to create objects .
Syntax :public static long multiplyExact(long num1, long num2)
Parameters : The method accepts a two parameters :
- num1: This is of long type representing one parameter
- num2: This is of long type representing another parameter
Return Value : The method returns the product of num1 and num2.
Exception : It throws ArithmeticException when the result overflows a long .
Examples :Input: num1 = 64954 nm2 = 6643 Output: 431489422
Below program illustrate the Java.lang.StrictMath.multiplyExact() method.
// Java praogram to illustrate the// Java.lang.StrictMath.multiplyExact()importjava.lang.*;classGeeks {publicstaticvoidmain(String args[]){longnum1 =772810, num2 =22929, num3 = -81827;longnum4 = -823783;System.out.println("Product of "+ num1 +" and "+ num2 +" is "+ StrictMath.multiplyExact(num1, num2));System.out.println("Product of "+ num3 +" and "+ num4 +" is "+ StrictMath.multiplyExact(num3, num4));System.out.println("Product of "+ num2 +" and "+ num4 +" is "+ StrictMath.multiplyExact(num2, num4));}}chevron_rightfilter_noneOutput:Product of 772810 and 22929 is 17719760490 Product of -81827 and -823783 is 67407691541 Product of 22929 and -823783 is -18888520407
Recommended Posts:
- Java multiplyExact() in Math
- StrictMath log() Method In Java
- StrictMath sin() Method in Java
- StrictMath tan() Method in Java
- StrictMath pow() Method in Java
- StrictMath exp() Method in Java
- StrictMath log1p() Method in Java
- StrictMath cos() Method in Java with Examples
- StrictMath hypot() Method in Java
- StrictMath random() Method in Java
- StrictMath cbrt() Method in Java
- StrictMath getExponent() Method In Java
- StrictMath fma() method in Java with Examples
- StrictMath atan2() Method in Java
- StrictMath toRadians() Method in Java
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.



