File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Postfix Operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Postfix Operator" Watch "Postfix Operator" New topic
Author

Postfix Operator

Jisha Anand
Ranch Hand

Joined: Apr 02, 2009
Posts: 62

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!




SCJP, SCWCD, SCBCD
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2379
    
    7

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


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
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?


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2379
    
    7

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

Joined: Oct 13, 2005
Posts: 38405
    
  23
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

Joined: Feb 10, 2009
Posts: 710
EDIT: Too late. Campbell's explination cleared it up.

Thanks!
Jisha Anand
Ranch Hand

Joined: Apr 02, 2009
Posts: 62

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

Joined: Sep 13, 2007
Posts: 2379
    
    7

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

Joined: Apr 02, 2009
Posts: 62

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

Joined: Apr 02, 2009
Posts: 62

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
Sheriff

Joined: Sep 28, 2004
Posts: 18745
    
  40

This issue is also explained in one of the JavaRanch FAQs.

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

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jisha Anand
Ranch Hand

Joined: Apr 02, 2009
Posts: 62

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

Joined: Oct 13, 2005
Posts: 38405
    
  23
Well done.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Postfix Operator