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"
     }
}
4

11 回答 11

73
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.

于 2011-03-24T01:11:11.490 回答
18

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"
于 2013-11-18T06:30:50.553 回答
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.

于 2013-07-09T06:15:05.733 回答
2
System.out.println(i++);  // "6"

This sends println the value I had prior to this line of code (6), and then increments I (to 7).

于 2011-03-24T01:08:13.430 回答
2

Why wouldn't the variable have been updated?

  • Postfix: passes the current value of i to the function and then increments it.
  • Prefix: increments the current value and then passes it to the function.

The lines where you don't do anything with i make no difference.

Notice that this is also true for assignments:

i = 0;
test = ++i;  // 1
test2 = i++; // 1
于 2011-03-24T01:09:11.753 回答
1

I know this is a super old question, but I didn't see this type of answer listed. Looking at an example of how the actual operators is implemented is helpful for me, maybe it would be helpful for someone else

class Integer {
  private int __i;

  function Integer ++() { // prefix operator i.e. ++x
    __i+=1; //increment
    return this; //return object with incremented value
  }

  function Integer ++(Integer x) { //postfix operator i.e. x++
    __i+=1; //increment
    return x; //return original object
  }
}
于 2019-10-03T16:13:03.800 回答
0

Well think of it in terms of temporary variables.

i =3 ;
i ++ ; // is equivalent to:   temp = i++; and so , temp = 3 and then "i" will increment and become     i = 4;
System.out.println(i); // will print 4

Now,

i=3;
System.out.println(i++);

is equivalent to

temp = i++;  // temp will assume value of current "i", after which "i" will increment and become i= 4
System.out.println(temp); //we're printing temp and not "i"
于 2013-05-25T04:05:09.213 回答
0

Maybe you can understand better Prefix/postfix with this example.

public class TestPrefixPostFix 
{
    public static void main (String[] args)
    { 
        int x=10;
        System.out.println( (x++ % 2 == 0)?"yes "+ x: " no "+x);
        x=10;
        System.out.println( (++x % 2 == 0)?"yes "+ x: " no "+x);
    }
}    
于 2015-03-11T16:31:38.163 回答
0

This is my answer. Some of you may find it easy to understand.

package package02;

public class C11PostfixAndPrefix {

    public static void main(String[] args) {
        // In this program, we will use the value of x for understanding prefix 
        // and the value of y for understaning postfix. 
        // Let's see how it works. 

        int x = 5; 
        int y = 5; 

        Line 13:   System.out.println(++x);  // 6   This is prefixing. 1 is added before x is used. 
        Line 14:   System.out.println(y++);  // 5   This is postfixing. y is used first and 1 is added. 

        System.out.println("---------- just for differentiating");

        System.out.println(x);  // 6   In prefixing, the value is same as before {See line 13}
        System.out.println(y);  // 6   In postfixing, the value increases by 1  {See line 14} 

        // Conclusion: In prefixing (++x), the value of x gets increased first and the used 
        // in an operation. While, in postfixing (y++), the value is used first and changed by
        // adding the number. 
    }
}
于 2017-01-04T00:27:21.820 回答
0

It prints 7 for the last statement, cos in the statement above, it's value is 6 and it's incremented to 7 when the last statement gets printed

于 2018-02-12T19:22:00.787 回答
0

This might help.. it also took my to understand this puzzle..

public class Main { public static void main(String[] args) {

int x = 5;
int y=5;

System.out.println(++x);
System.out.println(y++);

System.out.println("------");

System.out.println(x);
System.out.println(y);

} }

于 2021-04-02T07:49:40.453 回答