• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Post increment and the assignment operator

 
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic