• 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

post increment

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi javarachers



The output for the above is :1,0
I was expecting as 0,1.
My understanding was,post increment is always done after the end of statement.but in the above code inceremented i value is passed to m(i) before the and of statement.
The above code is from link and question number 22web page

Please can anyone explain the above.

thanks
shakunthala Divakar
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Shakunthala!

One of your first posts, what?

Welcome to the ranch!



your problem of understanding seems to be in the second line of these:
int i = 0;
i = i++ + m(i);
System.out.print(i);

this combined operations are evaluated from left to right, so first

now m runs with the argument 1.
It first prints this 1 (first output is one and the comma)
and returns 0.
The 0 is added to the "old" i (still 0) so i = 0 + 0 = 0
And this is the second output (zero).

Note that always
i=i++;
will never change the value of i, but in between there is a hidden copy of i that is one higher, but after the line it will be lost.



Phew!


Yours,
Bu.
 
shakunthala Divakar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Burkhard Hassel

Thank you very much for spending your precious time.
your reply was very useful for me.

(that was my second post)

Thanks
Shakunthala Divakar
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic