Scala Tutorial Scala References

Scala - Math incrementExact() Method



The Scala Math incrementExact() method returns the argument increased by one. 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 incrementExact(x: Int): Int = java.lang.Math.incrementExact(x)
def incrementExact(x: Long): Long = java.lang.Math.incrementExact(x)

Parameters

x Specify the value.

Return Value

Returns the argument increased by one.

Exception

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

Example:

In the example below, Math.incrementExact() method is used to increase the argument by one.

object MainObject {
  def main(args: Array[String]) {
    var x1 : Int = 10;
    var x2 : Long = 100;
    println("Math.incrementExact(x1) = "
            + Math.incrementExact(x1)); 
    println("Math.incrementExact(x2) = "
            + Math.incrementExact(x2)); 
  }
}

The output of the above code will be:

Math.incrementExact(x1) = 11
Math.incrementExact(x2) = 101

❮ Scala - Math Methods

5