StrictMath.incrementExact(long): This method is available in java.lang.StrictMath class of Java.
Syntax:
long java.lang.StrictMath.incrementExact(long a)
This method takes one argument of type long as its parameter. This method returns the argument incremented by one,throwing an exception if the result overflows a long. The overflow only occurs for the maximum value.
Parameters: One parameter is required for this method.
a: the value to increment.
Returns: the result.
Throws:
ArithmeticException - if the result overflows a long.
Approach 1: When no exceptions.
Java
public class StrictMathincrementExactlong {public static void main(String[] args) {long a = 1911119;System.out.println(StrictMath.incrementExact(a));}}
Output:
1911120
Approach 2: ArithmeticException
Java
public class StrictMathincrementExactlong {public static void main(String[] args) {long a = Long.MAX_VALUE;System.out.println(StrictMath.incrementExact(a));}}
Output:
Exception in thread "main" java.lang.ArithmeticException: long overflow at java.base/java.lang.Math.incrementExact(Math.java:987) at java.base/java.lang.StrictMath.incrementExact(StrictMath.java:864)
No comments:
Post a Comment