Close

Java - Math.multiplyExact() Examples

Java Java API 


Class:

java.lang.Math

java.lang.Objectjava.lang.Objectjava.lang.Mathjava.lang.MathLogicBig

Methods:

public static int multiplyExact(int x,
                                int y)

Returns the product of the arguments, throwing an exception if the result overflows an int.



public static long multiplyExact(long x,
                                 int y)

Returns the product of the arguments, throwing an exception if the result overflows a long.



public static long multiplyExact(long x,
                                 long y)

Returns the product of the arguments, throwing an exception if the result overflows a long.


Examples


package com.logicbig.example.math;

public class MultiplyExactExample {

public static void main(String... args) {
findMultiplyExact(41, 25);
findMultiplyExact(357, 5);
findMultiplyExact(-11, 13);
findMultiplyExact(0, 5);
findMultiplyExact(-3, -13);
}

private static void findMultiplyExact(int x, int y) {
int product = Math.multiplyExact(x, y);
System.out.printf("Product of %s amd %s = %d%n", x, y, product);
}
}

Output

Product of 41 amd 25 = 1025
Product of 357 amd 5 = 1785
Product of -11 amd 13 = -143
Product of 0 amd 5 = 0
Product of -3 amd -13 = 39




package com.logicbig.example.math;

public class MultiplyExactExample2 {

public static void main(String... args) {
findMultiplyExact(31, 155000l);
findMultiplyExact(7, 1021451l);
findMultiplyExact(1, Long.MAX_VALUE);
findMultiplyExact(2, Long.MAX_VALUE);
findMultiplyExact(Integer.MAX_VALUE, 2);
}

private static void findMultiplyExact(int x, long y) {
long product = 1;
System.out.println("-----------");
System.out.println("Result without Math.multiplyExact: ");
System.out.printf("%s * %s = %s%n", x, y, x * y);
System.out.println("Result with Math.multiplyExact: ");
try {
product = Math.multiplyExact(x, y);
} catch (ArithmeticException e) {
System.out.println("error: " + e);
}
System.out.println(product);
}
}

Output

-----------
Result without Math.multiplyExact:
31 * 155000 = 4805000
Result with Math.multiplyExact:
4805000
-----------
Result without Math.multiplyExact:
7 * 1021451 = 7150157
Result with Math.multiplyExact:
7150157
-----------
Result without Math.multiplyExact:
1 * 9223372036854775807 = 9223372036854775807
Result with Math.multiplyExact:
9223372036854775807
-----------
Result without Math.multiplyExact:
2 * 9223372036854775807 = -2
Result with Math.multiplyExact:
error: java.lang.ArithmeticException: long overflow
1
-----------
Result without Math.multiplyExact:
2147483647 * 2 = 4294967294
Result with Math.multiplyExact:
4294967294




package com.logicbig.example.math;

public class MultiplyExactExample3 {

public static void main(String... args) {
findMultiplyExact(30001l, 15000l);
findMultiplyExact(1l, Long.MIN_VALUE);
findMultiplyExact(-442, 133);
findMultiplyExact(100, 25);
findMultiplyExact(-300, -53);
}

private static void findMultiplyExact(long x, long y) {
long product = Math.multiplyExact(x, y);
System.out.printf("Product of %s amd %s = %d%n", x, y, product);
}
}

Output

Product of 30001 amd 15000 = 450015000
Product of 1 amd -9223372036854775808 = -9223372036854775808
Product of -442 amd 133 = -58786
Product of 100 amd 25 = 2500
Product of -300 amd -53 = 15900




See Also