Reputation: 146
this is my first question here, and I'm glad to join the community. I have a test tomorrow, and in one of the examples there is that line
i=0; j=2; z=2
i=j--;
What is the exact operation that is done? Because I know that j-- means j-1 everytime. Thanks! I'm using Dr.Java.
EDIT: It was a duplicate. Should I delete?
Upvotes: -3
Views: 5510
Reputation: 51
This means that i will have as value 2, because you used a post-decrement operator. J will have the value of 1 since it is decremented. If you had written:
i = --j
then i will have the value of 1, since the value is decremented BEFORE assignment.
Gerald
Upvotes: 0
Reputation: 7415
It is another shortcut for this code.
i=0; j=2; z=2
i = j;
j = j - 1;
Upvotes: 1
Reputation: 1074038
i = j--;
is an assignment statement. The right-hand side is evaluated, and the resulting value is assigned to the thing on the left-hand side. In your case, that means:
j
is read (2
, in that code)j
is decremented (to 1
, in that code)2
) is assigned to i
The order of Steps 1 and 2 is because the --
is after the j
; it's the postfix decrement operator. If it were before the j
(the prefix decrement operator), steps 1 and 2 would be reversed.
Upvotes: 6