Categories
java post-increment pre-increment

Java: Prefix/postfix of increment/decrement operators?

36

From the program below or here, why does the last call to System.out.println(i) print the value 7?

class PrePostDemo {
     public static void main(String[] args){
          int i = 3;
          i++;
          System.out.println(i);    // "4"
          ++i;             
          System.out.println(i);    // "5"
          System.out.println(++i);  // "6"
          System.out.println(i++);  // "6"
          System.out.println(i);    // "7"
     }
}

3

  • 7

    I believe I somewhat understand where your misunderstanding comes from. You believe a new value will only be assigned to i when it’s a statement on its own? When passing arguments to functions, the statements (in this case post and prefix) are executed before passing them. Add the behavioral difference between postfix and prefix as explained in the answers below, and you understand why you get that output.

    Mar 24, 2011 at 1:22


  • possible duplicate of What is x after “x = x++”?

    – nawfal

    Jul 20, 2014 at 8:56

75

i = 5;
System.out.println(++i); //6

This prints out “6” because it takes i, adds one to it, and returns the value: 5+1=6. This is prefixing, adding to the number before using it in the operation.

i = 6;
System.out.println(i++); //6 (i = 7, prints 6)

This prints out “6” because it takes i, stores a copy, adds 1 to the variable, and then returns the copy. So you get the value that i was, but also increment it at the same time. Therefore you print out the old value but it gets incremented. The beauty of a postfix increment.

Then when you print out i, it shows the real value of i because it had been incremented: 7.

1

  • 3

    All other answers here say that the value of “i” will be used first and then it will be incremented but well as you said is correct that value gets incremented and then the COPY of old value is returned. We understand more when we take example of i = 5;i = i++; If the value was assigned first and incremented then i would be 6 but in this case is 5

    – Saumyaraj

    Jul 15, 2016 at 18:34

20

I know this has been answered, but thought another explanation may be helpful.

Another way to illustrate it is:

++i will give the result of the new i, i++ will give the result of the original i and store the new i for the next action.

A way to think of it is, doing something else within the expression. When you are printing the current value of i, it will depend upon whether i has been changed within the expression or after the expression.

    int i = 1;
result i = ++i * 2 // result = 4, i = 2

i is evaluated (changed) before the result is calculated. Printing i for this expression, shows the changed value of i used for this expression.

result i = i++ * 2 // result = 2, i = 2

i is evaluated after the result in calculated. So printing i from this expression gives the original value of i used in this expression, but i is still changed for any further uses. So printing the value for i immediately after the expression, will show the new incremented value of i. As the value of i has changed, whether it is printed or used.

result i = i++ * 2 // result = 2, i = 2
System.out.println(i); // 2

If you kept a consistent pattern and included print lines for all the values:

  int i = 3; 
System.out.println(i);    //  3
System.out.println(i++);  //  3
System.out.println(i);    // "4"
System.out.println(++i);  //  5          
System.out.println(i);    // "5"
System.out.println(++i);  // "6"
System.out.println(i++);  // "6"
System.out.println(i);    // "7"

0

    5

    Think of ++i and i++ as SIMILAR to i = i+1. But it is NOT THE SAME. Difference is when i gets the new increment.

    in ++i , increment happens immediately.

    but if i++ is there increment will happen when program goes to next line.

    Look at code here.

    int i = 0;
    while(i < 10){
       System.out.println(i);
       i = increment(i);
    }
    
    private int increment(i){
       return i++;
    }
    

    This will result non ending loop. because i will be returned with original value and after the semicolon i will get incremented but returned value has not been. Therefore i will never actually returned as an incremented value.