Saturday, October 29, 2011
Can you explain Java pre/post increment/decrement?
If you are only incrementing or decrementing a variable, there's no difference.
i++;
++i;
These are equivalent statements in java.
If, however, you are assigning a value to a variable such as:
array[pos++] = 5;
This means, set the array element at position pos to 5 then increment pos by 1.
array[++pos] = 5;
This means, increment pos by 1 then set the array element at that new pos value to 5.
So if 'pos' was 3, in the first statement we're setting array[3] = 5. In the second statement we're setting array[4] = 5. In either situation, the next line of code will see 'pos' as 4.
This basically means if you are using pre-increment such as ++i this is evaluated before the rest of that code line is. If using post-increment such as i++ this is evaluated after the rest of the code line is.
http://answers.yahoo.com/question/index?qid=20100211211451AACctJj
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment