• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Tricky construction (i += i++)

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, guys! Could someone, please, explain me why this code:

will show "0" as a result? I supposed it'll show "1"

And question number two: one guy told me that operation of post-increment (i++) executes slower than (++i), because the first one demands a creation of temporary variabe. I found this old topic, but they were talking about C++ and there were few opposite points of view, so I didn't get whether it is true or not. Could you please clear things up for me?
 
Saloon Keeper
Posts: 14508
325
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i += i++
(i += (i++))
(0 += (i++))
(0 += (0++))
(0 += 0)
0

Technically it's very possible that i++ executes slightly slower than ++i. The difference however will be so incredibly small that you don't need to worry about it. Just use the correct statement for the correct situation.
 
Aleksey Vladimirovich
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:i += i++
(i += (i++))
(0 += (i++))
(0 += (0++))
(0 += 0)
0


But what's happening to i++ operation? I thought it should've incremented 0 after assignment...I can't get why it's not happening Could you please explain more in details?
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aleksey Vladimirovich wrote:
But what's happening to i++ operation? I thought it should've incremented 0 after assignment


It would have been incremented. But the expression is evaluated left-to-right. And the effect of that is that the initial value of i has effectively been saved before it's incremented. So the final assignment just stamps over the top of that increment.

Here's an alternative way of stepping through it that might demonstrate.

I'd recommend never actually doing things like this - don't mix increment operators and assignment. As you can see, it can rapidly lead to confusion.
 
Aleksey Vladimirovich
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I get it! Thank you, guys! Appreciate it
 
Marshal
Posts: 76885
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done, searching , but you were unlucky with what you found, even allowing for its being 7 years old. C++ is a different language, and the behaviour of different operators is different. For example, in C, that expression is not strictly defined at all, and getting an output of 1 or 0 is possible. Both values are permitted by the C language. But not in Java, which has stricter specifications. We have an FAQ, and there is something in the Java Tutorials, but the Java Language Specification is by no means easy to understand.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i = 0;
i += i++;
i = i + i++;
i = 0 + 0


Thanks.
reply
    Bookmark Topic Watch Topic
  • New Topic