• 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

order of evaluation/execution

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Someone recently pointed out that the following prints out the number 10, not 11 as expected:
int i = 10;
i = i++;
System.out.println("i: " + i);
my "guess" as to what is happening:
1. i (on the RHS) is evaluated (it is 10).
2. i (the variable) is incremented (becomes 11).
3. The assignment (due to the equals sign) occurs, using the evaluated value from step 1, not the variable.
But if that is the case, then why does the post increment occur before the assignment?
Thanks for any insight!
Tony
(tjackson@selectica.com)
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i++ is occuring after the variable has been assigned to i. It's just the way java handles pre/post increment operations in an assignment statement. You are correct, to get the desired behavior, use the pre-increment.
Bosun
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tony,
You are absolutely correct in the three steps you have written out! The post increment happens before the assignment because it has a higher priority then the assignment.
The compiler can only follow the JVM specs. It can't try and figure out what the programmer actually wanted.
Just to prove to yourself that the increment happens before the assignment try the following code:

The result will be 21 because the increment has the highest priority on the righthand side. The order will be
1. Save 10 into memory
2. Increment i to 11
3. Add saved value (10) to current value or i (11)
4. Print out result --> 10 + 11 = 21
Remember, the JVM compiler will always use the priority table and the assignment will (almost always) be performed last!
Regards,
Manfred.
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, I admit to posting a completely different message about half an hour ago, and I've edited this one.. so apologies to anyone that read that old one and wanted to refer to it. But really, it was kinda muddled anyways.

Here is the only part of the old post that survived:
I think this might be the answer (from RHE):

In Java, unlike many other languages, the apparent order of evaluation of operands in an expression is fixed. Specifically, the result of any statement will be as if all operands are evaluated left to right, even if the order of execution of the operations is something different, or if some optimization makes other changes to the reality.

So what this mostly says to me is... now matter what the precedence is, always perform atomic units of 'evaluation' from left to right. Also note, that in the first example given, there is only one 'operand', and I think that things are made more clear when there are two evaluations, and also when you compare the results of assigning to 'self' and assigning to 'not self'...
It is helpful to also write down my thoughts as I go through one example. I've made it a code block so it keeps indenting.


[This message has been edited by Mike Curwen (edited April 17, 2001).]
 
Author
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a matter of precedence. ++ has higher precedence than =.
java guarantees that every operand "appears to be fully
evaluated " before any part of the operation is performed.
the postfix increment assigns 11 to i, but delivers 10 as
its value. this is tehn assigned to i.
interestingly, this statemetn is not permitted in ansi C.
------------------
Fred Hosch
Author of:
An Introduction to Software Construction with Java
 
reply
    Bookmark Topic Watch Topic
  • New Topic