Java Math incrementExact() Method

Java Math incrementExact() method

Java Math Class

Java Math class provides several methods to perform several operations on math calculations like max(), min(), sin(), cos(), round(), ceil(), floor(), abs() etc.
The java.lang.Math class contains various methods for performing basic numeric operations

Here, in the page we will discuss about the math incrementExact() Method in java.

Java Math incrementExact() Method :

The incrementExact is a method in the Math class in Java that increments a value by one and throws an exception if the result overflows the range of the data type. It is similar to the  ++  operator, but it throws an exception if the result is outside the range of the data type rather than wrapping around.
 
Note: The data type of the argument should be either int or long.

Syntax  :

public static int decrementExact(int a)
public static long decrementExact(long a)

Example 1 :  

Run
public class Main {
    public static void main(String[] args) {
        // example with int data type
        int x = 100; // initialize x to 100

        try {
            // increment x by one and assign the result to x
            x = Math.incrementExact(x);
        } catch (ArithmeticException ex) {
            // if an ArithmeticException is thrown, print a message
            System.out.println("Overflow occurred (int).");
        }

        System.out.println(x); // prints 101

        // example with long data type
        long y = 100L; // initialize y to 100 (note the "L" suffix to specify a long literal)

        try {
            // increment y by one and assign the result to y
            y = Math.incrementExact(y);
        } catch (ArithmeticException ex) {
            // if an ArithmeticException is thrown, print a message
            System.out.println("Overflow occurred (long).");
        }

        System.out.println(y); // prints 101
    }
}

Output :

101
101

Explanation :

In this example, we first demonstrate how to use incrementExact with an int data type. We initialize the variable x to 100 and then call the incrementExact method on x, which increments x by one and assigns the result back to x. Since x is within the range of an int data type, the incrementExact method does not throw an exception and the value of x is updated to 101.

We then demonstrate how to use incrementExact with a long data type. We initialize the variable y to 100 and then call the incrementExact method on y, which increments y by one and assigns the result back to y. Since y is within the range of a long data type, the incrementExact method does not throw an exception and the value of y is updated to 101.

Example 2:  

Run
public class Main {
    public static void main(String[] args) {
        int x = Integer.MAX_VALUE; // initialize x to the maximum value for an int

        try {
            // increment x by one and assign the result to x
            x = Math.incrementExact(x);
        } catch (ArithmeticException ex) {
            // if an ArithmeticException is thrown, print a message
            System.out.println("Overflow occurred.");
        }

        System.out.println(x); // this line will not be reached
    }
}

Output :

Overflow occurred.

Explanation :

In this example, we initialize the variable x to the maximum value for an int data type. When we call incrementExact on x, it increments x by one, but since the result is outside the range of an int, an ArithmeticException is thrown. The exception is caught by the catch block, which prints a message to the console. The line System.out.println(x) will not be reached, since it is after the catch block

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription