• 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

Postfix Operator

 
Ranch Hand
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,
I have a silly doubt regarding postfix operator '++'

int myVal = 1;
myVal = myVal++;
System.out.println("MYVAL: "+myVal);


I expected the value of myVal to be 2, but the value was 1. Why is this so?

My guess was that the first line of the code would be split as:

myVal = myVal;
myVal = myVal + 1;


This should print MYVAL as 2 right? Why din't this happen?

Please help me!



 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose you have variable i. And you do i++ vs ++i. What's the difference?

i++ is assign to i then increment
++i is increment then assign back to i

So say i=0 to start with.


This also goes for i-- and --i
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This still looks funny to me. If I did myVal = myVal++, I know that myVal would be assigned the value for myVal then the increment happens. But shouldn't the increment increase the value of myVal, which would then be outputted?
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

W. Joe Smith wrote:This still looks funny to me. If I did myVal = myVal++, I know that myVal would be assigned the value for myVal then the increment happens. But shouldn't the increment increase the value of myVal, which would then be outputted?


Good point. OK if you do System.out.println(i++); you get 0. But now if you do that same line again what you get.... a 1. So doing the actual increment on the output line vs increment one line 1 then output on line 2 is different.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This comes up frequently, but you can't search for "++"! It is something which causes no end of confusion.

And you have to remember the operator precedences, which you can remind yourself of here.

When you haveyou set the value of 1 in i. Now, i++ increases the value of i by 1, before anything else happens, but it has its own value as well. So even though the value of i is now 2, the value of i++ as a whole is still 1, the old value. So writingtakes the value of i++ which we have already seen is 1, and applies that value to i. This happens last, so the value of 2 vanishes into cyber-limbo never to be seen again.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EDIT: Too late. Campbell's explination cleared it up.

Thanks!
 
Jisha Anand
Ranch Hand
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:Suppose you have variable i. And you do i++ vs ++i. What's the difference?

i++ is assign to i then increment
++i is increment then assign back to i

So say i=0 to start with.


This also goes for i-- and --i



Dear Tsang,

Thanks for your reply. But the point is still not very clear for me. What you have explained is how postfix operator works. I understand that point - postfix works only after the line in which it's defined gets executed. But in my case, myVal should be assigned a value and then should be incremented. So by the time it reaches the next line the value should be incremented right?

If the case was:

int myVal = 1;
int x = 1;
myVal = x++;
System.out.println("MYVAL: "+myVal+" "+"X: "+x);


The result will be:
MYVAL: 1 X: 2

My doubt is why doesn't the increment happen for myVal in a similar manner in the previous code?
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jisha Anand wrote:If the case was:

int myVal = 1;
int x = 1;
myVal = x++;
System.out.println("MYVAL: "+myVal+" "+"X: "+x);


The result will be:
MYVAL: 1 X: 2

My doubt is why doesn't the increment happen for myVal in a similar manner in the previous code?



So why myVal is not 2? Because x assigns itself (a 1) to myVal then increment (x) itself.
 
Jisha Anand
Ranch Hand
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:When you haveyou set the value of 1 in i. Now, i++ increases the value of i by 1, before anything else happens, but it has its own value as well. So even though the value of i is now 2, the value of i++ as a whole is still 1, the old value. So writingtakes the value of i++ which we have already seen is 1, and applies that value to i. This happens last, so the value of 2 vanishes into cyber-limbo never to be seen again.



Dear Campbell,

Thanks a lot for the reply. But I think I still did not get the point . Poor learning capacity you know

You mean to say that if we write then the first thing that is done is to increment i by 1? In that case, it's against the rule for postfix right? i++ is incrementing i by 1 and assigning it to i right? That is

Whether this increment and assignment takes place initially or at the last, i should be incremented in both cases right?
 
Jisha Anand
Ranch Hand
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:

So why myVal is not 2? Because x assigns itself (a 1) to myVal then increment (x) itself.



That is correct Tsang, I got it. But in the other case 'myVal' takes the place of 'x' right? If x got incremented by 1, then why din't myVal in get incremented by 1?

Sorry, this might be silly but I really got everything confused I think
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This issue is also explained in one of the JavaRanch FAQs.

http://faq.javaranch.com/java/PostIncrementOperatorAndAssignment

Henry
 
Jisha Anand
Ranch Hand
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:This issue is also explained in one of the JavaRanch FAQs.

http://faq.javaranch.com/java/PostIncrementOperatorAndAssignment

Henry


Dear Henry,
The link says, the following is what happens when is executed:



But then this is a deviation from the general principle of postfix operation right? Or is it that I got it all wrong?

If we say what really happens is:



Is this what you are saying is that happens? And if it's prefix operation then instead of 'oldValue' the real value of the variable will be substituted right? I think I got it!

Thanks a lot Henry! This really helped me in getting the concept
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done.
 
reply
    Bookmark Topic Watch Topic
  • New Topic