This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes increment operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "increment operators" Watch "increment operators" New topic
Author

increment operators

Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
According to the Sun tutorial, the precedence of the increment operators is as follows:

the postfix operator, as in i++, has the highest precedence
the prefix operator ++i is considered to be a unary operator and has precedence lower than that of the postfix operator

On the other hand, the prefix operator is applied before the expression in which it appears is evaluated; whereas the postfix operator is not applied until after the expression of which it is a part is evaluated.

So I tried the expression: j = ++i++; where both i and j had been declared as integers, and i had been initialized at 1.

I received the compile error: "unexpected type; expected variable; found value".

Is this kind of expression flagged in order to avoid the potential confusion of "++i++"? Or is there something else I'm missing here?

Thanks!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Jinny Morris:
...I tried the expression: j = ++i++; where both i and j had been declared as integers, and i had been initialized at 1.

I received the compile error: "unexpected type; expected variable; found value"...

Incrementors and decrementors can only be applied to variables, because the result is assigned back to that variable. They cannot be applied to values.

In evaluating the expression ++i++, first ++i is evaluated. This results in a value, and you cannot apply the postfix incrementor to a value.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
marc -

Thanks!

So further experimentation suggests that the only "expression" to which I can legitimately apply the increment and decrement operators is an expression consisting of a single variable (optionally adorned by a unary "+" or "-"); that is:

++(i + i) => compiler error

j = i + i;
++j; => clean compile

Sound right?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Surely it's i++ first, then ++i?? But it is reassuring that the compiler can recognise ++i++ as nonsense!
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Campbell -

No, the error message flags the postfix operator - suggesting that its parsing algorithm picked up the prefix operator first, and recognized (++i) as a value, so it flagged the postfix operator as incorrect syntax.

It was exactly that confusion (in my mind) that led to the experiment; the postfix operator is given higher precedence in the table, but my understanding of the actual order of evaluation is/was "apply prefix first, then evaluate assignment expression, then apply postfix". So I wanted to see what it would do if I tried to apply both -

Thanks!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jinny Morris:

On the other hand, the prefix operator is applied before the expression in which it appears is evaluated; whereas the postfix operator is not applied until after the expression of which it is a part is evaluated.


That is, in fact, not true - both operators are applied at the same time - it's just that the value of the expression is different.

More specifically, the postfix operator is applied as part of the expression (that is, the variable is incremented while the expression is being evaluated), *but* the *value* of the postfix expression is that of the variable before the increment.

See http://faq.javaranch.com/view?PostIncrementOperatorAndAssignment


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Ilja -

Thank you for the explanation and the link - the situation makes a lot more sense now!
 
Don't get me started about those stupid light bulbs.
 
subject: increment operators
 
Similar Threads
increment!
Operators Precedence
Operator Precedence qstn.
Why include all operators in the precedence hierachy?
operator precedence