incrementExact() int in Java

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

Syntax:

int java.lang.Math.incrementExact(int a)

This method takes one argument of type int as its parameter. This method returns the argument incremented by one, throwing an exception if the result overflows an int. The overflow only occurs for the maximum value. 

Parameters: One parameter is required for this method.

a: the value to increment.

Returns: the result

Note: It Throws: ArithmeticException - if the result overflows an int.

For Example:

Math.incrementExact(10) = > It returns 11.

Approach

Java

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

        int a = 10;
        System.out.println(Math.incrementExact(a));

    }
}

Output:

11

No comments:

Post a Comment