Java Math decrementExact() Method

Java Math decrementExact() 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 decrementExact() Method in java.

Java Math decrementExact() Method :

Java decrementExact() is a method in the Math class in Java that decrements 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 decrementExact() is a static method, so you can call it directly on the Math class without creating an instance of Math. It is available in Java 8 and later.
 
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 {
            // decrement x by one and assign the result to x
            x = Math.decrementExact(x);
        } catch (ArithmeticException ex) {
            // if an ArithmeticException is thrown, print a message
            System.out.println("Overflow occurred (int).");
        }

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

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

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

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

Output :

99
99

Explanation :

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

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

Note that when declaring a long literal, you need to use the L suffix to specify that it is a long rather than an int. This is not necessary for int literals.

Example 2:  

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

        try {
            // decrement x by one and assign the result to x
            x = Math.decrementExact(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.
-2147483648

Explanation :

In this example, we initialize the variable x to the minimum value for an int data type. When we call decrementExact on x, it decrements 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