• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

operator precedence question

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ran across this issue while studying for the ocp11 cert. The unary post increment/decrement are supposed to have the highest precedence then followed by pre increment/decrement, but this doesn't appear to be the case when I run this tiny sample.

the output I get is:
\ocp11_practice\1z0-815>java Sample.java
lion : 5 tiger : 36

so even with the parenthesis java appears to be processing the pre-increment first.
from experimentation it appears to be that increment/decrement operators have equal precedence whether pre or post and always higher than parenthesis. So that java processes all increment/decrement left to right before anything else.
 
Marshal
Posts: 74376
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please search; this question has come up several times in the last year. I seem to have linked to a successful search here. Scroll up and see the otheer information Oksana Cherniavskaia found in the same thread.

It is a mistake to think that high precedence operators are executed first or that the runtime finds high precedence operators first. Execution is principally left to right; brackets and associativity and precedences determine which operator operates on an operand with two operators. In the original version of this code, if there isn't an overflow error the answer is always 5.
 
David Kobrin
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed in the original example, because which ever is processed first you are always multiplying and dividing by the same number. which is why I changed the example. However if the operator precedence does not affect the processing order then what is the point? Only by processing in precedence order do you achieve the correct answer in an equation, otherwise addition could occur before multiplication, etc. I think this is a java error to separated the pre and post versions on different tiers when the compiler treats them as the same tier. So, either java implementation should change to match the documentation or the documentation should change to match how the compiler treats them. In the mean time though how are we supposed to prepare for the test?
 
David Kobrin
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so I've been doing some more digging, and found a post on the issue on stack overflow operator precedence so it looks like the discussion in the book needs to be re-worked a bit. The example:

will always evaluate to 5, but does so in this order:


basically the only time you could observe the higher precedence of the postfix version would be if you could use both pre and post on the same operand (++x--) which is illegal because once x-- is processed it is a value not a variable. Therefore to simplify the issue to one that we can deal with for the test treat prefix and postfix as the same tier of precedence and apply them left to right.
 
Campbell Ritchie
Marshal
Posts: 74376
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you have got the order of execution right.
You do indeed perceive the results of precedences; the appearance of lion on the left has two operators, prefix ++ and multiplication, so you have to deal with their precedences. It is possible to apply other prefix unary operators to a postfix expression, just not ++i and --i. For example the following is legal:-Don't forget the other prefix unary operators + ~ and !. I presume you have read the Java® Language Specification (=JLS) link in that helpful SO thread;. Did you read the five links I gave you? You will find other people have had the same confusion.
In your code, although lion-- hs a higher precedence than ++lion, the left operand to * is evaluated first, so the increment is applied before the multiplication. But remember this is exam practice code; the same JLS link you found on the SO thread says, one line down,

It is recommended that code not rely crucially on this specification. Code is usually clearer when each expression contains at most one side effect, as its outermost operation, and when code does not depend on exactly which exception arises as a consequence of the left-to-right evaluation of expressions.

I shall add you to our exam forum.
reply
    Bookmark Topic Watch Topic
  • New Topic