StrictMath.decrementExact(int): This method is available in java.lang.StrictMath class of Java.
Syntax:
int java.lang.StrictMath.decrementExact(int a)
This method takes one argument of type int as its parameter. This method returns the argument decremented by one, throwing an exception if the result overflows an int.
Parameters: One parameter is required for this method.
a: the value to decrement.
Returns: the result.
Throws:
ArithmeticException - if the result overflows an int.
Approach 1: When no exceptions.
Java
public class StrictMathdecrementExactint {public static void main(String[] args) {int a = 178;System.out.println(StrictMath.decrementExact(a));}}
Output:
177
Approach 2: ArithmeticException
Java
public class StrictMathdecrementExactint {public static void main(String[] args) {int a = Integer.MIN_VALUE;System.out.println(StrictMath.decrementExact(a));}}
Output:
Exception in thread "main" java.lang.ArithmeticException: integer overflow at java.base/java.lang.Math.decrementExact(Math.java:1006) at java.base/java.lang.StrictMath.decrementExact(StrictMath.java:879)
No comments:
Post a Comment