1

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

E_net4
  • 27,810
  • 13
  • 101
  • 139
  • Do you have any code that you think proves that one has higher precedence over the other? – Rohit Jain Jan 20 '14 at 13:02
  • Either accept precedence from C or go ask James Gosling or Dennis Ritchie. Usually asked by people who want to do heinous things and offer code examples that no rational person would ever write. I see that it actually goes back to B, the predecessor to C. – duffymo Jan 20 '14 at 13:03
  • @Rohit Jain Here's proof that post-incrementing does in fact have higher precedence than pre-incrementing in Java (referring to 1st and 2nd level on this table) http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html – Mistylaneous Jan 20 '14 at 13:06
  • Can you at least motivate your WHY with a counterproposal? What is so surprising there? Was it obvious that they should have had the *same* precedence? Should the precedence have been the other way around? – Marko Topolnik Jan 20 '14 at 13:09
  • Just to warm you up: `int i = Integer.MAX_VALUE; ++i--;` Is the precedence irrelevant? – Marko Topolnik Jan 20 '14 at 13:11
  • 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 – Mistylaneous Jan 20 '14 at 13:12
  • Had you motivated your question with what you and I have written above, you would have (*maybe*) shown the audience that you have something substantial to ask about. – Marko Topolnik Jan 20 '14 at 13:19

1 Answers1

1

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    The relative precedence of pre- and post-incrementing in Java does not matter. However, for efficiency of presentation, all prefix operators including pre-incrementation have the same precedence. One of them is unary minus. Now consider the expression **- x++**. It is parsed as **- (x++)**, which makes sense. With the opposite precedence it would be parsed as **(-x)++**, which is undefined because **-x** is not an lvalue (i.e. cannot be assigned to). –  Jul 17 '19 at 23:58
  • If you want the question reopened, start a reopen vote. – Stephen C Jul 18 '19 at 02:57
  • I am not interested in the administration of this site. I just keep seeing things that have been closed too aggressively and occasionally point this out. –  Jul 18 '19 at 09:46
  • Noted. But it is not relevant to my answer, so please make your comments on meta. – Stephen C Jul 18 '19 at 11:50