aspose file tools*
The moose likes Beginning Java and the fly likes Increment Operator Precedence Failure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Increment Operator Precedence Failure" Watch "Increment Operator Precedence Failure" New topic
Author

Increment Operator Precedence Failure

Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
Can someone explain to me why this does not not work? According to the documentation ++ has precedence of 2 while >= has a precedence of 6.

The expected output is:

the actual output is:

if I initialize i to 10 I get:

It sure seems to me that the ++ operator is not executing with a higher precedence than >=.
When I change the code to this, I get exactly what I expect:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
This sort of question comes up all the time. The i++ operator has the highest precedence of all, shared with i--, with only separators like () and . having higher precedence.
What you are misinterpreting (as everybody else seems to), is what the ++ increments. It alters the value of i but does not alter the value of i++. Yes, you have two values. The value of i behind the scenes might be 10, but the value of i++ that you are using is actually 9.
A similar problem is discussed in this FAQ.
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
Dopey me. Of course that is way it works. What I should have done was:

Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
You’re welcome

You should always be aware of that sort of side‑effect.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Campbell Ritchie wrote:You should always be aware of that sort of side‑effect.


And in my opinion, you should avoid writing code which relies on it. If I was working on that code, I would have to stop and ask myself what it did. And I would probably have to go off and read something like the Ranch's FAQ to make sure I understood it correctly.

And code which makes me do that is dangerous -- I might just do what you did and incorrectly assume I knew what it did -- and time-wasting -- I have to stop, figure out what it does, and then try to pick up my original train of thought. Better to rewrite it in such a way that you don't use the value of i++ or ++i.
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
I understand the sentiment but that is a pretty common construct in other languages (C, Perl, PHP, etc.) and very commonly used. Indeed I use so much that I take it for granted and somehow ignored all my programming experience with it in this case I guess because I am a Java beginner.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

Dennis Putnam wrote:Can someone explain to me why this does not not work? According to the documentation ++ has precedence of 2 while >= has a precedence of 6.


It sure seems to me that the ++ operator is not executing with a higher precedence than >=.


Also note that the conclusion isn't correct -- just because the value isn't what is expected doesn't mean that the precedence "failed". If the ">=" operator indeed did have a precedence greater than the "++" operator, then you wouldn't get that value. Heck, it wouldn't compile, as the ">=" operator won't have a left operand.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Dennis Putnam wrote:I understand the sentiment but that is a pretty common construct in other languages (C, Perl, PHP, etc.) and very commonly used.


It is a particular case of the general rule of thumb that functions with side-effects are evil. Generally if you have a function which returns a value, it should just return that value and not also change something else as well. As you can see, both i++ and ++i return values and also change the value of their operand. So they are evil.

Of course we write functions which are supposed to change things all the time -- changing things is why we write the programs containing those functions, after all. But functions which change things should just do that and nothing else. Returning values from such functions just confuses the matter and makes it harder to understand code which contains such functions.

And that's my point. The languages you mentioned there are all notorious for the way they make it easy to produce "write-only" code, i.e. code which can be easily written but not easily understood. I'm here to say you should produce easily-understood code.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
The only reason you see more write‑only C code than Java code is that is is (or used to be) fashionable to write C code to use the fewest keystrokes possible. Maybe to conserve memory.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Increment Operator Precedence Failure