StrictMath.multiplyExact(int, int): This method is available in java.lang.StrictMath class of Java.
Syntax:
int java.lang.StrictMath.multiplyExact(int x, int y)
This method takes two arguments of type int as its parameters, This method returns the product of the arguments, throwing an exception if the result overflows an int.
Parameters: Two parameters are required for this method.
x: the first value.
y: the second value.
Returns: the result.
Throws:
ArithmeticException - if the result overflows an int.
Approach 1: When no exceptions.
Java
public class StrictMathmultiplyExactint {public static void main(String[] args) {int x = 156, y = 99;System.out.println(StrictMath.multiplyExact(x, y));}}
Output:
15444
Approach 2: ArithmeticException
Java
public class StrictMathmultiplyExactint {public static void main(String[] args) {int x = 15886, y = 988889;System.out.println(StrictMath.multiplyExact(x, y));}}
Output:
Exception in thread "main" java.lang.ArithmeticException: integer overflow at java.base/java.lang.Math.multiplyExact(Math.java:909) at java.base/java.lang.StrictMath.multiplyExact(StrictMath.java:804)
Some more multiplyExact() Methods of StrictMath class.
StrictMath.multiplyExact(long, long)
StrictMath.multiplyExact(long, int)
No comments:
Post a Comment