• 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

unary operator

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
everybody
i have some confusion while working on post++ operater.
output of following code results 0,while according to
me it should be 2.
int i=0;
i=i++;
i=i++;
i=i++;
because after first line i should be 1,and after second
statement is executed i should be two;
please explain;
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This has been talked about many times in this forum. Do a search for i=i++ to get some great discussions on it. But basically it has to do with assignment presedence and since assignment has the lowest presedence you are getting something like i = 1 = 0, so you go backwards and i ends up as 0 again. I don't think that is the technical way of putting it, but that is how I remember it.
Bill
 
sanpan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks bill
but sorry i am not satisfied with your answer.
you say
i=0,i=i++
will convert into i=1=0 but i think since on i post increment
operator is applied so it should be first evaluvated then
incremented . so it should be i=0=1;
please help me .

thanks in advance
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sanpan
Here's what happens with postfix operator:
<pre class="code">
int x = 0;
x = x++; <span class="red">// result: 0, x is not incremented</span>

original value of x is saved (x0rig)
x is incremented
x0rig is assigned to x
therefore, x will always equal original value
</pre>
Hope that helps.
Moving this thread to Certification Study.
------------------
Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Here you need to understand the concept of assignment operator.
Everytime you are trying to assign the post - incremented value of i++ to i the value is never assigned..Hence you get 0.
i=0;
i=i++; Here the value of i is 0 Since i++ is not incremented.
i=i++; Though previous i++ has increased to 1 it's not assigned to i Hence still i remains 0. Same is the case for all the 3 lines..
This explains you query..
Cheers Johnson
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic