- 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()
import
java.lang.*;
class
Geeks {
public
static
void
main(String args[])
{
int
num1 =
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));
}
}
Output: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()
import
java.lang.*;
class
Geeks {
public
static
void
main(String args[])
{
long
num1 =
2727810
, num2 =
9892829
;
int
num3 =
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));
}
}
Output: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()
import
java.lang.*;
class
Geeks {
public
static
void
main(String args[])
{
long
num1 =
772810
, num2 =
22929
, num3 = -
81827
;
long
num4 = -
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));
}
}
Output:Product of 772810 and 22929 is 17719760490 Product of -81827 and -823783 is 67407691541 Product of 22929 and -823783 is -18888520407
StrictMath multiplyExact() Method in Java
Recommended Articles