aspose file tools*
The moose likes Java in General and the fly likes Post increment and the assignment operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Post increment and the assignment operator" Watch "Post increment and the assignment operator" New topic
Author

Post increment and the assignment operator

Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1197
Here's a code snippet from a mock SCJP exam question:
int i = 100 ;
i = i++ ;
i = ++i ;
i = i-- ;
i = i++ ;
System.out.println( i ) ;
the value of i that gets printed out is 101, much to my surprise. It took me forever to understand the explanation, because they could have said that the assignment operator has precedence over post-increment/post-decrement, therfore the value stored in i doesn't change. Instead, they gave some arcane reason that I didn't understand. Now that I get it, my question is this: What happens to the increment/decrement that occurs after assignment? Does it happen and then disappear into the ether after the statement executes, or does it not happen at all?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Does it happen and then disappear into the ether after the statement executes

Yup, that's about it. You know you can call a method that returns a value without using the value. This is valid code:

The toUpperCase method returns a new String value. I'd guess it goes on the heap, but I make a point of not caring. Since it is never assigned into a variable, it is unreferenced and a candidate for garbage collection.
BTW: because the example you gave is confusing and an easy opportunity to make a mistake, some teams have coding standards that only allow these operators on lines by themselves.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Charles Keller
Greenhorn

Joined: Jan 31, 2004
Posts: 24
To Carol Murphy,
I had trouble myself with the postfix notation, which in i=i++, did not do anything, except if you have a=2;b=a++ the result is a=2 and b=3.
So, I created the attached IncDecrementTrial4.java file, together with the output in Word. Please let me know what you think.

Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
Carol,
int i = 100
i = i++;
i++ does happen but only when the whole statement has finished.
it is like saying i = i; then increment the i that had the ++ operator, so you assigned i to 100;
the method that i understood it with was this:

Does this help you, it would be the same with the -- operator.
I know it is not what you really asked but it shows you how the post and pre-incrementer work.
so you can add your code to it.
int i = 100 ;
i = i++ ; //i = 100 the post-increment did not get done (effectively)
i = ++i ; //i = 101 the pre-increment did
i = i-- ; //i = 101 the post-decrement did not get done (effectively)
i = i++ ; //i = 101 the post-increment did not get done (effectively)
System.out.println( i ) ; //i = 101
Davy
[ February 27, 2004: Message edited by: Davy Kelly ]

How simple does it have to be???
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
The way I look at these pre/post increment/decrement operators is as follows. When you perform one of these, think of the compiler creating a new variable and: (a) copying the value, and (b) running the increment/decrement operation. The order of (a) and (b) changes depending on whether it's a post or pre operation.
So:

...would be equivalent to...

(I only included the {}'s to show that the temporary variable j goes immediately out of scope and doesn't exist outside the expansion that the compiler introduced...that's why you can't get at that temporary value and it just gets dropped out of existence.)
Now let's look at the expanded version of preincrement:

...is equivalent to...

If you always do those expansions in your head whenever you see the code, you can always figure out what's going on even in complicated operations like:

This looks really daunting at first, but if you know that the + operator is left associative (i.e. this statement will evaluate first at the rightmost +, and continue to crawl to the left), you can break it down step by step. And in this case, you see the effect of the temporary variable int this case because when it's returned, it doesn't simply go out of scope before getting used--it is that temporary variable that's used in place of the evaluated operand.
So you should be able to see with a little consideration how the above code expands to:

Now you can easily expand the expressions for temp2 and temp3 as the rule above shows (we'll deal with temp1 after):

Now we have everything expanded except that first line, which I left till now because it has two of these little increment nasties. But it's not that bad, actually...you just make sure you expand the right most operand before the left:

Now if we put this whole mess together, this:

expands to this:

And now a final runthrough to see what x and k end up as. I'll put a comment after each line whenever a value changes:

Fascinating, no? (Well, maybe not.)
sev
[ February 29, 2004: Message edited by: sever oon ]
 
 
subject: Post increment and the assignment operator