• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

increment operators

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surely it's i++ first, then ++i?? But it is reassuring that the compiler can recognise ++i++ as nonsense!
 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ilja -

Thank you for the explanation and the link - the situation makes a lot more sense now!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic