multiplyExact() long in Java

multiplyExact(): This method is available in the Math class of Java.

Syntax:

long java.lang.Math.multiplyExact(long x, long y)

This method takes two arguments of type long as its parameters. This method returns the product of the arguments.

Note:

It throwing an exception if the result overflows a long.

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 a long.

For Example:

Math.multiplyExact(191919, 19191) = > It returns 3683117529

Approach

Java

public class MultiplyExactLong {
    public static void main(String[] args) {

        long x = 191919, y = 19191;
        System.out.println(Math.multiplyExact(x, y));

    }
}

Output:

3683117529

No comments:

Post a Comment