Interesting facts about Increment and Decrement operators in Java

Increment operator

It is used to increment a value by 1. There are two varieties of increment operator:

  • Post-Increment : Value is first used for computing the result and then incremented.
  • Pre-Increment : Value is incremented first and then result is computed.

Decrement operator



It is used for decrementing the value by 1. There are two varieties of decrement operator.

  • Post-decrement : Value is first used for computing the result and then decremented.
  • Pre-decrement : Value is decremented first and then result is computed.

Interesting facts about Increment and Decrement operators

  1. Can be applied to variables only : We can apply ++ and — operator only for variables but not for the constant values. If we are trying to apply ++ and — operator on constant value then we will get compile time error.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // Increment and Decrement operators
    // Can be applied to variables only
      
    public class Test {
        public static void main(String[] args)
        {
            int a = 10;
            int b = ++a;
      
            // uncomment below line to see error 
            // b = 10++;
      
            System.out.println(b);
        }
    }

    chevron_right

    
    

    Output:

    11
    

    Un-comment to see error:

     
    prog.java:10: error: unexpected type
            b = 10++;
                ^
      required: variable
  2. Nesting of both ++ and — operators is not allowed
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // nesting can not be 
    // applied to ++ and --
      
    public class Test {
        public static void main(String[] args)
        {
            int a = 10;
            int b = ++(++a);
            System.out.println(b);
        }
    }

    chevron_right

    
    

    Output:

    error: unexpected type
    
  3. For final variables we can’t apply increment and decrement operator : The increment and decrement operators can not be applied to final variables because of the simple reason that their value can not be changed.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // Increment and Decrement operators
    // Can not be applied to final variables
      
    public class Test {
        public static void main(String[] args)
        {
            final int a = 10;
            int b = ++a;
            System.out.println(b);
        }
    }

    chevron_right

    
    

    Output:

    error: cannot assign a value to final variable a
    
  4. Increment and Decrement Operators can not be applied to boolean : We can apply ++ and — operators for all primitive data types except Boolean.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // Increment and Decrement operators
    // Can not be applied boolean data type
    public class Test {
        public static void main(String[] args)
        {
            boolean b = false;
            b++;
            System.out.println(b);
        }
    }

    chevron_right

    
    

    Output:

    error: bad operand type boolean for unary operator '++'
    

This article is contributed by Bishal Kumar Dubey. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up