• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

assignment question

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Was playing around and was expecting the result to be
0
1
2
3
.
.
.
Instead I get an infinite 0's

any ideas?
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change:
i = i++;
to:
i = ++i;
or:
i++;
or:
++i;

In your scenario, "i" is committed to set itself to "i" before "i" is incremented. "i" is then incremented and then set back to what it committed itself to in the first place, which is itself before incrementing.
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not convinced you just reiterated what happened. Could you explain further.
 
Marc Peabody
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My wording was confusing and I'm still not sure if I really wrote what I meant.
For simplicity's sake, let's change your code to not use the "for" loop:

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

Now let's take this in steps.
Step 1) "i" is initialized to 0 and we print it out

Step 2) "++" has higher precedence than "=", so it is done first and "i" is incremented.
Step 3) "=" now evaluates "i++", which is 0 (the postfix operator evaluates to what it was before incrementing) and this 0 is set to "i"

Step 4) 0 is printed because "i" is 0

In C/C++, your loop would give you what you expected though.
 
Marc Peabody
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The most important word in my last post is evaluates.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In C/C++, your loop would give you what you expected though.



No, in C++ the result of i=i++ is undefined. On some compilers it will evaluate to i++, on others to an empty expression (as it does in Java).
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For SCJP you must forget C++, drink Java.

Corey's note on this topic
[ December 09, 2004: Message edited by: Barry Gaunt ]
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i = i++;

so i++ is evaluated first before the assignment. But if thats the case should'nt it be 1. Wow this question is now confusing me? I always look at i++ as evaluate first then iterate. Therefore I see it as i is assigned 0 but then i is incremented by 1.

Still lost
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read Corey's article as pointed out by Barry. My understanding after reading the article . Let's take a look at the line
i = i++; ( assuming i is initialized to 0 )

Step 1> i++, this is post increment, so assign the value of i which is zero in this case to some temporary memory location ( My assumption of temp memory location could be wrong..

Step 2> Now increment the value of i, i = i + 1; which yields i = 1;

Step 3> Finally perform the assignment, where i is assigned the value in the temp memory location which sets i back to 0 ( Zero )

Looks like this is what is happening under the hood, but again I could be wrong with the assumption of temp memory location.

Can somebody show/explain how the values go on stack ?
[ December 09, 2004: Message edited by: Jay Pawar ]
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i = i++;

1. Retrieve the value of i from memory and save it for later use.
2. Increment i in memory by 1.
3. Store the value saved from step 1 in i.

So i gets incremented, but the new value of i is immediately overwritten by the saved old value of i. The retrieval of i and the increment of i must take place next to each other because post-increment is really one operation with two steps. The assignment operation has lower precedence and takes place after the post-increment operation is complete.
 
Marc Peabody
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To further beat the dead horse...
If you have:
int i = 0;
System.out.println(i++); // evaluates to 0, even though i becomes 1
System.out.println(i); // i is now 1

The output will be:
0
1

This is the difference of using ++i(pre) VS i++(post) - they both increment the value but the postfix one evaluates to the value BEFORE incrementing while the prefix one evaluates to the value AFTER incrementing.

Moral of the story: Don't send a postfix to do a prefix's job.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic