Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Tricky construction (i += i++) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Tricky construction (i += i++)" Watch "Tricky construction (i += i++)" New topic
Author

Tricky construction (i += i++)

Aleksey Vladimirovich
Ranch Hand

Joined: Sep 05, 2012
Posts: 56
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

Joined: Sep 20, 2010
Posts: 3986
    
  18

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.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Aleksey Vladimirovich
Ranch Hand

Joined: Sep 05, 2012
Posts: 56
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

Joined: Apr 06, 2010
Posts: 4541
    
    8

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

Joined: Sep 05, 2012
Posts: 56
Now I get it! Thank you, guys! Appreciate it
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43935
    
  33
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

Joined: Sep 14, 2012
Posts: 13
i = 0;
i += i++;
i = i + i++;
i = 0 + 0


Thanks.
 
Have you checked out Aspose?
 
subject: Tricky construction (i += i++)
 
It's not a secret anymore!