This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Postfix Operator

 
Jisha Anand
Ranch Hand
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
  • 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!



 
K. Tsang
Bartender
Posts: 3521
16
Android Java
  • Mark post as helpful
  • send pies
  • 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
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • 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: 3521
16
Android Java
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 3521
16
Android Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 21501
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic