• 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

assignment operators

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get a very wierd problem about assigment operator about the code below:

int i=0;
i=i++;
System.out.println(i);

The result printed out is 0.

I don't know why it is not 1. Can someone explain how this happens?
Thanks a lot!
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's post increment, the assignment happens and then i is incremented.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PostIncrementOperatorAndAssignment
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused with the reply.
int i =0;
i = i++;
System.out.println(i);
Here is what I think the sequence of operations is
1. i=0 and this value is assinged to the RHS and hence i is zero.
2. i is then incremented due to the post fix increment.
Shouldn't i then become 1.

Thanks
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Posted by Michael Carlson
It's post increment, the assignment happens and then i is incremented.



Its a misconception among ppl that assignment occurs before increment but its wrong.


If u see the precedence order, postfix operator has higher precedence than assignment. So i++ is executed first.






Here is the order of execution.

1. i++ gets evaluated, so i becomes 1 but still the value of the expression i++ is zero.

U can test it by...

System.out.println(i++); it prints 0. if i is zero initially



Even in method call,

meth(i++, i) means-->>> meth(0, 1) if i is initially zero.

jst remember this. first VM stores the value of i which is zero to some temporary location then increments its value by one. So i becomes one, but the value which is assigned to i is again temporary location value.

Hence i again becomes zero.

Naseem
[ May 31, 2006: Message edited by: Naseem Khan ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic