Java Math.multiplyExact() method

The java.lang.Math.mutliplyExact() returns the product of the arguments. It will throw an exception if the result overflows either int or long.

Syntax

Parameter

Return

  • If one of the arguments is Integer.MAX_VALUE or Integer.MIN_VALUE or Long.MAX_VALUE or Long.MIN_VALUE, it will throw an ArithmeticException.

Example 1

Test it Now

Output:

3695

Example 2

Test it Now

Output:

Exception in thread "main" java.lang.ArithmeticException: long overflow
	at java.lang.Math.multiplyExact(Math.java:892)
	at multiplyExactExample2.main(multiplyExactExample2.java:8)

Example 3

Test it Now

Output:

Exception in thread "main" java.lang.ArithmeticException: integer overflow
	at java.lang.Math.multiplyExact(Math.java:867)
	at MultiplyExactExample3.main(MultiplyExactExample3.java:8)

Next TopicJava Math




Latest Courses