tags:

views:

651

answers:

8

** Dup: http://stackoverflow.com/questions/226002/whats-the-difference-between-x-x-vs-x **

So, even though I know you would never actually do this in code, I'm still curious:

public static void main(String[] args) {
    int index = 0;
    System.out.println(index);   // 0
    index++;
    System.out.println(index);   // 1
    index = index++;
    System.out.println(index);   // 1
    System.out.println(index++); // 1
    System.out.println(index);   // 2
}

Note that the 3rd sysout is still 1. In my mind the line index = index++; means "set index to index, then increment index by 1" in the same way System.out.println(index++); means "pass index to the println method then increment index by 1".

This is not the case however. Can anyone explain what's going on?

+5  A: 

this is a duplicate question.

EDIT: I can't seem to find the original :P oh well

a = a++ uses the postincrement, which your compiler interprets as:

a = function() {
   var old_value = a;
   a++;
   return old_value;
}

EDIT 2: http://stackoverflow.com/questions/226002/whats-the-difference-between-x-x-vs-x

Jimmy
What's the link to the original one?
MK_Dev
can't find it :( I'm very certain I've seen it before though :<
Jimmy
+2  A: 

The assignment occurs after the expression has been evaluated. So index++ has a value of 0, although as a side effect index is incremented. Then the value (0) is assigned to index.

recursive
oic, so the ++ occurs after resolving index, not after the (index = index) has executed. If the latter were true it would result in index being incremented.
SCdF
+3  A: 

value++; is post increment.

int firtValue = 9;
int secondValue = firstValue++;

firstValue is now 10, but secondValue is 9, the value of firstValue before it was incremented.

Now with pre-increment:

int firtValue = 9;
int secondValue = ++firstValue;

firstValue and secondValue are now 10, fistValue is incremented and then its value is asigned to secondValue.

CMS
+1  A: 

The post-increment operator index++ increments the variable, yet returns its old value, thus

int i = 5;
System.out.println(i++);

will print 5 yet i is now equal to 6.

if you want to return the value after the increment operation use ++index

Jeff Mc
A: 

The answer to this question should help. The post increment does update the value of index but it returns the value before the update.

Vincent Ramdhanie
A: 

I've never tried anything like this, but I'm willing to be that the assignment happens after the increment.

So what really happens as far as the compiler is concerned is:

  1. Evaluate index
  2. Keep the value of index for later
  3. Increment the value of index
  4. Assign the old value of index, thus wipe out the increment.
Uri
A: 

See http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html for information on the postfix evaluation rules.

Also see http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#39438 for information.

S.Lott
A: 

You have to look at the order things are evaluated.

in the following statement

index = index++;

three things happen 1) since it is index++, the value of index is determined 2) index is incremented, 3) the value that was determined in step one is then assigned to the variable on the left hand side of the equation

Kevin