Question

I understand how post-incrementing and pre-incrementing in Java works, and I've read countless other threads/questions from other people regarding this same topic. BUT! I have yet to learn WHY post-incrementing has higher precedence than pre-incrementing in Java.

Does anyone know?

To my knowledge, post-incrementing having higher or lower precedence than pre-incrementing will not affect your programs' results since there aren't other unary operators that could interfere with post/pre-incrementing. Furthermore, ++ isn't a binary operator, so it's not like it interferes with +, -, *, / and %. So therefore.. WHY? WHY does post-incrementing have higher precedence than pre-incrementing?

My motivation is this: Java's been made so that post-incrementing has a higher precedence than pre-incrementing and I haven't yet seen any example where the result differs depending on these rules. So I don't understand why post/pre-incrementing doesn't have the same level of precedence

I'd be very happy if anyone could enlighten me! :)

Here's one official site that states precedence rules/levels in Java: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

Was it helpful?

Solution

You would need to ask the Java designers to know why they made that design call.

But I expect that it is one or both of the following:

  • It is just for consistency with C and C++.
  • It is because the JLS doesn't directly specify operator precedence. Instead it is emergent from the way that the grammar is specified. As JLS 15.2 says:

    "Precedence among operators is managed by a hierarchy of grammar productions."

    In other words, it is the structure of the Java formal grammar that determines operator (etc) precedence, not some precedence table. And the Java grammar mirrors the structure of the specification document.

The "official site" you link to in your Question is actually part of the Oracle Java Tutorial. It is a secondary source. The JLS takes precedence.

See also:


Here is an example where the operator precedence would make an observable difference:

 public static volatile a = 42;

 public void busywork() {
    while (true) {
       ++aa--;   // ... assuming this was valid Java code ....
    }
 }

Now create 2 threads, one to call busywork, and the second to sample the values of a. You should be able to observe different sets of values for a, depending on the relative operator precedence; i.e. {41, 42} with the current rules. If the precedence was the same for post and pre-increment and decrement, then it would be compiler specific whether you saw {41, 42} or {42, 43}.

(And of course, having two valid parse trees is going to make life more complicated for the compiler specification and the compiler implementors.)


Finally, it is worth nothing that ++aa-- in the example above is invalid irrespective of the precedence. The increment / decrement operators require an <lvalue> and evaluate an <rvalue>, and you can't make the above expression fit those constraints for any ordering or precedence.

Therefore the fact that prefix and postfix forms are described as having different precedence makes no practical difference to programmers.

Maybe the authors of the Java Tutorial could have "simplified" the pre- and post- precedence groups into one group in that (non-definitive) table. They chose not to. You could suggest a correction through the appropriate channels if you were so inclined.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top