• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

post increment operator

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
i am confused.....
what would the code print??
int i=0;//1
i=i++;//2
System.out.println(i);//3
would it print 1 or 0? explain please!!!
thanks
bye
pradeep
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It prints 0.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can serach this forum because we have discussed this many times.
Here is how it works:
1) return the original value
2) increment i
3) assign the returned value from step 1 to i
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, but if 1 isn't correct, I'm going to have to beat a dead horse (such a horrible thing to do at a [java]ranch as I'm not even sure what terms could be searched to find the prior threads Thomas referenced!
According to this chart (and others):
http://www.sorcon.com/java2/precedence.htm
the ++ operator has precedence over =
However, it is a post-incrementer.
Therefore, here's what I would *think* would happen.
1. i = 0;
2. i = i++;
So although I know from what Thomas said that 1 is wrong, it looks to me as though 0 is first assigned to i in line 2 (the current value), and then i is incremented 1 due to the i++. Since it is mutable, that *should* result in a value of 1 in my mind.
So assuming it doesn't, why not? (or how can I find the threads that already discuss it since I'm not having any success doing so?)
Ross
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way I think of it is like this:
Since ++ has precedence over = then (i++) is performed first. At that point, the value of i is 1. However, the JLS specified that the postfix increment/decrement operators must return its original value. So the result of the expression (i++) is 0, which is finally assigned to i and overrides its current value of 1.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i = i++;
Add parentheses according to operator precedence.
i = (i++);
Evaluating the expression i++ has two steps.
(1) The first step is to determine the value of the expression. This is the value of the variable i, which is 0. Save this value somewhere safe.
(2) The second step (the side effect) is to increment the variable i. The variable i now holds the value 1.
After the right operand has been evaluated, assign the result to the variable i. Retrieve the saved value of the expression 0 and assign it to the variable i.
[ August 01, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some people like words. Other people like code.
 
Trust God, but always tether your camel... to this tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic