Scala Tutorial Scala References

Scala - Math multiplyExact() Method



The Scala Math multiplyExact() method returns product of its arguments. The method can be overloaded and it can take Int and Long arguments. It throws an exception, if the result overflows an Int or a Long.

Syntax

def multiplyExact(x: Int, y: Int): Int = java.lang.Math.multiplyExact(x, y)
def multiplyExact(x: Long, y: Long): Long = java.lang.Math.multiplyExact(x, y)

Parameters

x Specify the first value.
y Specify the second value.

Return Value

Returns product of its arguments.

Exception

Throws ArithmeticException, if the result overflows an Int or a Long.

Example:

In the example below, Math.multiplyExact() method is used to multiply given numbers.

object MainObject {
  def main(args: Array[String]) {
    var x1 : Int = 10;
    var y1 : Int = 20;
    var x2 : Long = 10;
    var y2 : Long = 20;
    println("Math.multiplyExact(x1, y1) = "
            + Math.multiplyExact(x1, y1)); 
    println("Math.multiplyExact(x2, y2) = "
            + Math.multiplyExact(x2, y2)); 
  }
}

The output of the above code will be:

Math.multiplyExact(x1, y1) = 200
Math.multiplyExact(x2, y2) = 200

❮ Scala - Math Methods