• 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 when using post-increment operator

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Guillermo Ishi wrote: . . . I was talking about the effect, not some analogy for the internals. The effect of i++ most definitely does not happen. . . .

What you wrote is at best confusing to those who are not experienced.

Although you can show that frame(S) ≡ frame(T)
and
S ⋄ E ≡ T ⋄ E
where
S = n = n++;
and
T =skip
Henry's program demonstrates that the effect of n++ does occur and can be seen.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That means that the two programs shown are equivalent to each other, but such equivalence assumes that is the entire program. The following two programs are also equivalent to each other, but the middle state definitely exists, even if only for a short time in one of them but not the other.The problem with n++ is that it is difficult to “observe the middle state.”
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If those two programs do not constitute the entire program, as has been demonstrated, then my program 1 is not equivalent to program 2.
i = i++; can only be only equivalent to the empty statement (or program 3 below) if it constitutes the entire program.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another example is concurrent code.

If one thread performs i = i++; and another performs System.out.println(i);, then the value printed may very well be i+1.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the case of concurrent code, the code shown in this discussion does not constitute the whole program and all the equivalences I mentioned earlier become incorrect. As Henry showed on the first page of this thread.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed. The "can't see it with one example, hence, it didn't happen" argument isn't very conclusive.

We know that it happens from the bytecode. We know that the parts of the expression happens from the many examples provided. And we know that it happens with the whole expression with the threaded example.

It would be very silly to resort to a debugger and assorted breakpoints to prove it actually happens.

Henry
 
Ranch Hand
Posts: 386
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is this witchcraft?

good thread

** edit: would it be correct to say then that the post increment occurs after evaluation, but not after the assignment of that evaluation? and that's why this makes my head hurt?! i think i understand it now.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nick woodward wrote:would it be correct to say then that the post increment occurs after evaluation, but not after the assignment of that evaluation? and that's why this makes my head hurt?! i think i understand it now.


Yes. The statement i = i++; is evaluated like
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion, the subject of this topic is misleading as this topic has little to do with operator precedence, and mainly to do with the Order of Evaluation.

Henry
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:In my opinion, the subject of this topic is misleading as this topic has little to do with operator precedence, and mainly to do with the Order of Evaluation.


True! I have updated the title accordingly.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic