GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is wrong with i = i++; Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is wrong with i = i++;" Watch "what is wrong with i = i++;" New topic
Author

what is wrong with i = i++;

reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287


Why the output of this code is 0? It explains 'The method fermin only receives a copy of the variable i and any modifications to it are not reflected in the version in the calling method. '. Isn't it Java pass-by-value? What's going on here?

I think i = i++; can be splitted to
i = i;
i++;

Why i's value is not incremented here?

Please help.


SCJA, SCJP5.0, SCBCD, SCWCD
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
>> I think i = i++; can be splitted to
>> i = i;
>> i++;

Not true. Keep in mind that you are incrementing an expression:

lvalue = rvalue; // two expressions; one statement

Now consider this:

int i = 0;
i = i++;

What is the value of the expression on the right? Remember the postfix is not applied until after the expression is evaluated and it's the result of the evaluation that gets assigned.

I admit that's a litle tricky how many times have you incremented a number and then assigned itself back to itself? Usually you would i++ without any assignment.
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
More succinctly, this:

i = i++;

... breaks down to this:

int value = i;
i++;
i = value;
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11350
    
  16

you pass the value of i to the function, in this case, 0. the method fermin then assigns that value to it's localy defined i. the local i is incremented, leaving the i back in main at 0. when we return from fermin(), the local i is dropped out of scope, and we go back to our main i.

then, we evaluate the Right Hand Side, which is 0, and remember that. we increment i from 0 to 1. we then assign the value of the RHS (0) to i, thus losing the value 1 we just got.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rick Portugal
Ranch Hand

Joined: Dec 17, 2002
Posts: 243
Originally posted by Rick O'Shay:
More succinctly, this:

i = i++;

... breaks down to this:

int value = i;
i++;
i = value;
That's not quite right because the assignment actually takes place before the incrementation. A better way to think about it is as two separate variables.

left = right;
right++;
System.out.println(left);

In other words, the right hand side of the expression was incremented but never used for anything.


IBM 286, SCJP, SCWCD, EIEIO
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
Originally posted by Rick Portugal:
That's not quite right because the assignment actually takes place before the incrementation. A better way to think about it is as two separate variables.

left = right;
right++;
System.out.println(left);

In other words, the right hand side of the expression was incremented but never used for anything.


False. The rvalue is computed, the postfix operator is applied to the variable, the rvalue is assigned to the lvalue. Not surprisingly that accurately describes the operators involved and the actual result.

What you are showing is not legal Java. The ++ and -- operators require a variable, not an expression. That would be "i" in this case.
Rick Portugal
Ranch Hand

Joined: Dec 17, 2002
Posts: 243
That's incorrect. This program is "legal Java" and functions exactly as "i = i++" does:It increments right yet doesn't effect the value of left. The result is zero, just as the lvalue result of "i = i++" was zero.

The reason that this is incorrect:

is that in the expression "i = i++", rvalue is assigned to lvalue BEFORE rvalue is incremented. The sequence above is incorrect because the assignment, "i = value", is done as the last step.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I believe Rick O'S is describing the process correctly. Rick P - how would you account for the output of this program?

As you can see, the method foo() has no effect on the value n which is passed in - that same value is returned from the method. So x = foo(x++) is effectively the same as x = x++, except that foo() gives us an opportunity to see the values of x and n along the way. From the output it's clear that when foo() executes, x has been incremented to 1 - and somehow afterwards, x is back to 0 again. This is explainable as follows:

In other words, the assignment to x takes place after the increment operator has been evaluated.
[ June 21, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Karthikeyan Balasubramanian
Ranch Hand

Joined: Apr 20, 2005
Posts: 89
everything is ok.....when we consider two variables.
but in case of single variable,
int i=0;
i=i++;
here i is assigned 0 first.ok..acceptable.
But wat happens to ++ operator.
i hope it increments the value of the variable..but not assigning to it.
any explanations?,...........


SCJP 1.4--93%<br />SCWCD 1.4--86%<br />Next--???
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

i++ increments i, but the expression i++ evaluates to the value of i before it is incremented. The assignment operator "=" has the least precedence; in other words, the assignment of the pre-increment value of i to the 'i' variable is the last thing to happen.

The moral? Don't write code like i=i++. It's confusing and silly.
Rick Portugal
Ranch Hand

Joined: Dec 17, 2002
Posts: 243
Agreed.

http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter02/expressions.html

k++ : returns k, then k is incremented by 1
...
(x++) the initial value of x is returned and then x is incremented.
..
in expressions such as y = x++;
...
y takes on the value of x before the increment occurs. If x is initially 3, then y becomes 3 and x becomes 4.
 
GeeCON Prague 2014
 
subject: what is wrong with i = i++;