Java Math incrementExact() method


The incrementExact() is a built-in function in java which returns the argument incremented by one, throwing an exception if the result overflows the specified datatype either long or int depending on which data type has been used on the method argument.

Syntax:

int incrementExact(int num)
long incrementExact(long num)

Parameter: The function accepts one mandatory parameter as shown above and described below:

  • num – The parameter specifies the number which has to be incremented.
  • Return Value: The function returns the argument incremented by one, throwing an exception if the result overflows the specified datatype either long or int depending on which data type has been used on the method argument.

    Examples:



    Input : 12
    Output : 13
    
    Input : -3 
    Output : -2
    

    Program 1: Program to demonstrate the working of function

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate working
    // of java.lang.Math.incrementExact() method
    import java.lang.Math;
      
    class Gfg1 {
      
        // driver code
        public static void main(String args[])
        {
      
            int y = 12;
            System.out.println(Math.incrementExact(y));
      
            int x = -3;
            System.out.println(Math.incrementExact(x));
        }
    }

    chevron_right

    
    

    Output:

    13
    -2

    Program 2: Program to demonstrate the overflow in function

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate overflow
    // of java.lang.Math.incrementExact() method
    import java.lang.Math;
      
    class Gfg1 {
      
        // driver code
        public static void main(String args[])
        {
      
            int y = Integer.MAX_VALUE;
            System.out.println(Math.incrementExact(y));
        }
    }

    chevron_right

    
    

    Output:

    Exception in thread "main" java.lang.ArithmeticException: integer overflow
        at java.lang.Math.incrementExact(Math.java:909)
        at Gfg1.main(File.java:12)

    Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.