java.lang.Math.incrementExact()
method is used to find the argument incremented by one of a int value for the given input (x
– parameter) in Java.
throwing an exception if the result overflows an int.
Package
incrementExact()
method exists in Math
class of java.lang
package.Since: JDK1.8
Method Declaration of incrementExact()
Method
The declaration of
incrementExact()
method is:
public static int incrementExact(int x)
Syntax of incrementExact()
Method
The syntax of
incrementExact()
method in Java is:
java.lang.Math.incrementExact(int x)
Example
/* *Find the argument incremented by one of given input(int) without importing the java.lang Package. */ public class JavaMathExample { public static void main(String[] args) { //Declaring variables int x = -4876.1874; int result; //Assign argument incremented by one of x to result variable result = java.lang.Math.incrementExact(x); //Printing result to the console System.out.println(result); } }
Output:
Example
By importing java.lang
package the syntax of incrementExact()
method can be used as:
/* *Find the argument incremented by one of given input(int) by importing the java.lang Package. */ import java.lang.*; //importing package public class JavaMathExample { public static void main(String[] args) { //Declaring variables int x = -68.97; int result; //Assign argument incremented by one of x to result variable result = Math.incrementExact(x); //Printing result to the console System.out.println(result); } }
Output:
Special Cases of incrementExact()
Method in Java
ArithmeticException – if the result overflows an int
Example
Example
/* *This example shows some special cases of incrementExact() method in Java */ import java.lang.*; //importing java.lang package public class JavaMathExample { public static void main(String[] args) { //Declaring variables int positiveZero = 0; int negativeZero = -0; int positiveInfinity = Int.POSITIVE_INFINITY; int negativeInfinity = Int.NEGATIVE_INFINITY; int nan = Double.NaN; int result; //Here argument is positive zero, output will also be positive zero result = Math.incrementExact(positiveZero); System.out.println(result); //Here argument is negative zero, output will be positive zero result = Math.incrementExact(negativeZero); System.out.println(result); //Here argument is positive infinity, output will also be positive infinity result = Math.incrementExact(positiveInfinity); System.out.println(result); //Here argument is negative infinity, output will be negative infinity result = Math.incrementExact(negativeInfinity); System.out.println(result); //Here argument is NaN, output will be NaN result = Math.incrementExact(nan); System.out.println(result); } }
Output:
Parameters of incrementExact()
Method in Java
x
– Where x
is any valid int input value. This parameter is required.
Error Handling
If the
If there is no argument (
x
parameter is not a number incrementExact()
method returns an error
.If there is no argument (
x
– input value) passes to the method, the compiler will produce an error
.Example
/* *This example returns a compile time error */ import java.lang.*; //importing java.lang package public class JavaMathExample { public static void main(String[] args) { //Declaring variables String x = "Not a number"; int result; //argument is not a number result = Math.incrementExact(x); System.out.println(result); //Here no argument is used result = Math.incrementExact(); System.out.println(result); } }
Output:
Error
Return Value of incrementExact()
Method in Java
incrementExact()
method returns the argument incremented by one of a int value for the given input (x
– parameter).
Java incrementExact()
Method Example 1
import java.lang.*; //importing java.lang package public class JavaMathExample { public static void main(String[] args) { int negativeValue = -18.869; int positiveValue = 27.78639; int zero = 0; System.out.println("argument incremented by one of a Negative Value: "+Math.incrementExact(negativeValue)); System.out.println("argument incremented by one of a Positive Value: "+Math.incrementExact(positiveValue)); System.out.println("argument incremented by one of Zero: "+Math.incrementExact(zero)); } }
Output:
Java incrementExact()
Method Example 2
import java.lang.*; //importing java.lang package public class JavaMathExample { public static void main(String[] args) { //Declare and initialize an array containing int values int test [] = { Int.MAX_VALUE, Int.MIN_VALUE, Int.NaN, Int.MIN_EXPONENT, 42.867e-12, 16.852, 0, -2.58, -78.8569e7 }; //Use for loop to print argument incremented by one of each element of the array for (int i=0; i < test.length; i++) { System.out.println( Math.incrementExact(test[i]) ); } } }
Output: