I just declare the data array
data = {0, 1, 2, 3, 4}, i = 1;
data[i++] = data[i++] * 2;
I test in Java and Javascript, the results both are
{0, 4, 2, 3, 4}
it's too strange to me, I think at first
data[i++] * 2, data[1] * 2 = 2,
then i
bees 2
, then data[2] = 2
and i
bee 3
. So result should be {0, 1, 2, 3, 4}
.
Could someone know the reason of this result?
I just declare the data array
data = {0, 1, 2, 3, 4}, i = 1;
data[i++] = data[i++] * 2;
I test in Java and Javascript, the results both are
{0, 4, 2, 3, 4}
it's too strange to me, I think at first
data[i++] * 2, data[1] * 2 = 2,
then i
bees 2
, then data[2] = 2
and i
bee 3
. So result should be {0, 1, 2, 3, 4}
.
Could someone know the reason of this result?
Share Improve this question edited Dec 6, 2013 at 9:27 Duncan Jones 69.4k32 gold badges201 silver badges259 bronze badges asked Dec 6, 2013 at 9:24 regrecallregrecall 5311 gold badge7 silver badges21 bronze badges 4- I removed your c and c++ tags as they appear irrelevant. If your question is actually about one of those languages, please explain why in your question. – Duncan Jones Commented Dec 6, 2013 at 9:27
- my expected result is {0, 1, 2, 3, 4}, not {0, 4, 2, 3, 4} – regrecall Commented Dec 6, 2013 at 9:34
- This question helps others to think in lot different way.. but ur expectation is not clear sorry – sam Commented Dec 6, 2013 at 9:38
- 2 Are you sure your expected result is {0, 1, 2, 3, 4}? Isn't that the input? – Yao Bo Lu Commented Dec 6, 2013 at 9:41
7 Answers
Reset to default 7From the Java Language Specification at http://docs.oracle./javase/specs/jls/se7/html/jls-15.html#jls-15.26.1
If the left-hand operand is an array access expression (§15.13), possibly enclosed in one or more pairs of parentheses, then:
First, the array reference subexpression of the left-hand operand array access expression is evaluated. If this evaluation pletes abruptly, then the assignment expression pletes abruptly for the same reason; the index subexpression (of the left-hand operand array access expression) and the right-hand operand are not evaluated and no assignment occurs.
Otherwise, the index subexpression of the left-hand operand array access expression is evaluated. If this evaluation pletes abruptly, then the assignment expression pletes abruptly for the same reason and the right-hand operand is not evaluated and no assignment occurs.
Otherwise, the right-hand operand is evaluated. If this evaluation pletes abruptly, then the assignment expression pletes abruptly for the same reason and no assignment occurs.
In other words, the array index on the left hand side MUST be evaluated before the right hand side, to conform to the Java Language Specification.
This means that you'll be assigning into data[1]
, and the value that you assign will be data[2] * 2
.
If you're asking about Javascript though, all I can suggest is that the makers of Javascript wanted to make the result match the result from Java.
it is an autoincrement, postincrement in it (i.e the current value of a variable is used but after using it , it's value automatically gets incremented for the next use).
in the first data[i++]
it uses the value of i (which is 1) and then increases it, hence in the second data[i++]
i is 2
Hence the equation bees data[1]=data[2]*2
hope it does make sense
data[i++] = data[i++] * 2;
step1:-
data[i++] -> data[1]//because i is under post increment
step2:-
data[i++] * 2->data[2]*2-> 2*2 ->4 // now i is 2
step3:-
data[1] = 4;//interpreter interprets left to right
In Java, an assignment of the form a[x] = y;
is evaluated left-to-right. That is, x
is evaluated before y
. The same is true for JavaScript.
Also, the post-increment operator x++
changes the value of x
after is has been used.
Combine these two facts with a visual explanation (below) and it should all make sense:
i = 1;
data[i++] = data[i++] * 2;
^ ^
| |
1 2
System.out.println(i); // would display '3'
Its pretty simple... (Post Increment Does the Trick)
The value of i++ in righthand side is changed to 2 since it has been Post-incremented in left hand side
data[i++]=data[i++]*2
which expands as follows:
data[1]=data[2]*2
data[1]=2*2;
hence
data=[0,4,2,3,4]
Javascript
data[i++] = data[i++] * 2; // i === 1
data[1] = data[i++] * 2; // i === 2
data[1] = data[2]*2; // i === 3
The ++ operator increments the variable and then returns (is evaluated to) the original value.
One simple example for it is:
var x = 0;
console.log(x + (x++) + x) // 1
The first and second x are evaluated to 0, while the third is 1 (because it has been incremented)
As other people have said: data[1] = data[2]*2 and once that expression is processed i==3. However, this means that what we have next is data[3] = data[4]*2 which should be 16. So your output does not make sense.