Java – Find Long Argument Incremented – incrementExact() Method with Examples

java.lang.Math.incrementExact() method is used to find the argument incremented by one of a long value for the given input (x – parameter) in Java.
throwing an exception if the result overflows a long.

incrementExact() method exists in Math class of java.lang package.
Since: JDK1.8

 

Declaration of incrementExact() Method

The declaration of incrementExact() method is:

public static long incrementExact(long x)

 

Syntax of incrementExact() Method

The syntax of incrementExact() method in Java is:

 
java.lang.Math.incrementExact(long x)

Example

/*
*Find the argument incremented by one of given input(long) without importing the 
java.lang Package.
*/
public class JavaMathExample {
    public static void main(String[] args) {
        //Declaring variables
        long x = 560;
        long 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:
561
Note that in the above example x is incremented by one (from 560 to 561)
Example
By importing java.lang package the syntax of incrementExact() method can be used as:

/*
*Find the argument incremented by one of given input(long) by importing the java.lang Package.
*/
import java.lang.*;  //importing package

public class JavaMathExample {
    public static void main(String[] args) {
        
        //Declaring variables
        long x = 90;
        long 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:
91

 

Parameters of incrementExact() Method in Java

x – Where x is any valid long input value. This parameter is required.

 

Exception Handling

If result overflows a long, arithmatic execption occurs.
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";
        long 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 long 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) {
        long negativeValue = -18.869;
        long positiveValue = 27.78639;
        long 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) {
        int a = 7689;
        long b = (long) a;  //casting int to long

        float c = 36532.232F;
        long d = (long) c; //casting float to long

        double x = 560.0;
        long y = (long) x; //casting double to long

        long z = 38_954_990_395_984_502L;


        long result;
        //Assign argument incremented by one of b to result variable
        result = java.lang.Math.incrementExact(b);
        //Printing result to the console
        System.out.println(b+ " incremented by one is " +result);

        //Assign argument incremented by one of d to result variable
        result = java.lang.Math.incrementExact(d);
        //Printing result to the console
        System.out.println(d+ " incremented by one is " +result);

        //Assign argument incremented by one of y to result variable
        result = java.lang.Math.incrementExact(y);
        //Printing result to the console
        System.out.println(y+ " incremented by one is " +result);

        //Assign argument incremented by one of z to result variable
        result = java.lang.Math.incrementExact(z);
        //Printing result to the console
        System.out.println(z+ " incremented by one is " +result);
    }
}

Output:

7689 incremented by one is 7690
36532 incremented by one is 36533
560 incremented by one is 561
38954990395984502 incremented by one is 38954990395984503

 

Java incrementExact() Method Example 3

import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {
        //Declare and initialize an array containing long values
        long test [] = { Long.MAX_VALUE, Long.MIN_VALUE, 
		Long.NaN, Long.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: