• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Pl clear the very basic funda!

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int i = 0;
i = i++;
System.out.println(i);
The above code in a main statement of a class give output as 0 not 1 why?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int i = 0;
i = i++;
System.out.println(i);
The above code in a main statement of a class give output as 0 not 1 why?

in i = i++, the assignment operator is done first (l to r), so i is asssigned to i and the '++' is effectively ignored, as it is a post operation increment. Compare this to just i++ on line (2), which gives your expected 1 result
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a simple breakdown will surely help u:
y=x++
-------
can be broken down to :
y=x
x=x+1
==============
y=++x
-----
can be broken down to :
x=x+1
y=x(nothing but x+1)
this is the best explanation I found after many years of confusion
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A good mnemonic for this:
j=i /* . . . . . . . */ ++ ;
(compiles and run)
Try this 4 sec training. For a permanent result.
Rythm it up (high or internal voice) 2 times as this:
i=i // say fast
... // wait..
++ // spit it now!
Now whenever you meet j=i++ in Mocks, the Test Center, or the real world, speak it:
j=i
...
++
JRoch

[This message has been edited by JRoch (edited April 07, 2000).]
[This message has been edited by JRoch (edited April 07, 2000).]
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sairam,
I have given an easy way out here. Check it out.
regds
maha anna
[This message has been edited by maha anna (edited April 07, 2000).]
 
I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic