• 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

Question of precedence

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why the answer is 0?
int i = 0;
i = i++;
System.out.println(i);
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think the answer should be?
 
sing
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i thought the answer is 1.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i = i++
1. the unary ++ is evaluated first, but since it's a post-increment, the value of that expression is the value of i before the increment, so this is zero.
2. As a side effect, the variable i is incremented by one, so it's one.
3. Now finally, the evaluated value of the expression on the right hand side (zero) gets stored in the variable in the left hand side. Thus i gets zero. The side effect in step 2 is "written over" basically.
Rob
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i = i++;


This statement is saying, set i equal to i, and then add 1 to i.
In order for the output in your example to be 1, you would need the following code:

I'm not sure how to articulate the answer in words, but that example should answer your question. Most often I think you see the use of ++ or -- used in counters inside of loops, which are rarely used in conjuction with an assignment statement, so a lot of people aren't aware that it can be used on either side.
--Chris
 
Chris Graham
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dang Rob, I thought I was the only one going through the message boards this late
--Chris
Sleep is for Weenies
[ January 09, 2002: Message edited by: Chris Graham ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hehe, sleep is for people that don't need to get their post counts up so they can win a free Java study guide ;p
 
Chris Graham
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We get a free study guide?
WHOOO!!!
after how many posts?
--Chris
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hehe, not quite that easy. The moderators will scan this board at the end of the week, and for each post someone has made they deem "worthy", they will enter that post into a raffle. They'll pick 4 winners at random, and each gets a copy of the Java 2 Exam prep. It should be on the top of your browser window!
 
sing
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes! i understand it. Thanks Rob and Chris.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic