• 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

how is this possible???

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope this is not a stupid question.
Why does this code print 0?
public class test {
public static void main(String[] args) {
int i = 0;
i = i++;
i = i++;
i = i++;
System.out.println(i);
}
}
David
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has to do with the order in which expresions are evaluated. I believe it works out like this:
1. calculate where results go
2. get the value of the expression
3. increment i
4. store the value from 2 in the location from 1
(thus over-writing the incremented i)
Bill

------------------
author of:
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David, always remember that there is some difference between the postfix and prefix.in ur code there is postfix i.e. the value is incremented after it is being assigned to itself or to some other variable.had it been prefix (++i) the output would be 3.
hope i have cleared ur doubt.
 
David Moore
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill,
When you say in line 3 increment i, it does not happen because
= is of higher presedence than ++, so the increment never happens because it's after i?
David
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,
The ++ operator has much higher percedence than assignment(=). Bill's steps are correctly ordered. The increment happens before (step 3) the assignment (step 4). Think of the postfix as using a special Java 'holding' area.
1. Place current value of i into holding area (move zero to holding area)
2. Increment current value of i (i now becomes 1)
3. Assign value in holding area to i (i now assigned zero)
Simple but effective,
Manfred.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Just visit the folowing thread.
http://www.javaranch.com/ubb/Forum24/HTML/008856.html
Same topic have been discussed there.
HOPE THAT WILL HELP U VERY MUCH.
<marquee> Ratul Banerjee </marquee>
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic