Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tricky construction (i += i++)

 
Aleksey Vladimirovich
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • 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?
 
Stephan van Hulst
Bartender
Pie
Posts: 5813
61
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Now I get it! Thank you, guys! Appreciate it
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
shivamahesh bachu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i = 0;
i += i++;
i = i + i++;
i = 0 + 0


Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic