• Post Reply Bookmark Topic Watch Topic
  • New Topic

Associativity and Precedence  RSS feed

 
Kenny Reid
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm currently going through chapter 3 of the Michael Ernest "Java SE7 Programming Essentials" which is concerned with associativity and precedence. I'm finding it very hard to get to grips with it.

Here is an extract

"Postfix operators take precedence over prefix operators. Two, postfix operators associate left
to right, while prefix operators associate right to left. In an expression that includes several of both, the postfix operators
would all evaluate right to left before the prefix operators, which would then evaluate in the reverse direction!"

I thought that postfix operators dont impact on a variable until after the expression completes, so why does it take "precedence"?
Or does it take precedence, but just not have any impact?

I also find the use of "associate right to left" confusing. For addition / multiplication you need to know which numbers the symbols apply
to (which I thought was what associativity was about), but for postfix and prefix, surely you know because its stuck on to the number
(ie either i++ or ++1)?

Thanks in advance.


 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Let's look at the Java Language specification. It says PostfixExpression ++, so the postfix operators do indeed associate to the left. But because
The result of the postfix increment expression is not a variable, but a value.
… you can only use a postfix operator once, so associativity does not apply.
Later on its says that unary operators group right‑to‑left. There are some combinations you cannot use together.
If you have unary and postfix operators combined, the postfix operators take precedence. You can writeWhich of course prints −123. You handle it as −(i++).
First apply the ++ operator which increments i but returns the (old) value 123 not 124. Yes, postfix ++ has two different effects.
Then apply the unary − operator and you get −123.

Postfix operators cause no end of confusion to lots of people. If you are confused, use the postfix operators on their own.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kenny Reid wrote:
I also find the use of "associate right to left" confusing. For addition / multiplication you need to know which numbers the symbols apply
to (which I thought was what associativity was about), but for postfix and prefix, surely you know because its stuck on to the number
(ie either i++ or ++1)?


Associativity is used to determine the order when operators have the same precedence. For example, the prefix increment operator, the unary minus, and the bitwise NOT all have the same precedence. So, to resolve this...



It has to use associativity, and do something like this...



And yes, "surely you know because its stuck on to the number" may be a valid statement. However, keep in mind that it is a big precedence / assoc table. It is better to do a full definition, than to leave sections of the table not in the JLS (because common sense has only one interpretation for it).

Henry


 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may not notice associativity problems like that, because all those operators are associative and commutative. But what about non‑commutative operators like −?
i − j ≠ j − i
How does this parse?
i − j − k
Put some values in i: 1, j: 3, k: 5 and evaluate that expression with different associativities. If you change the −s to +s you get the same result regardless because + is commutative.

And I don't think 1++ or similar is permissible because ++ and −− take variables as their operands not values.
 
Duggs Dojo
Greenhorn
Posts: 4
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that postfix operators dont impact on a variable until after the expression completes, so why does it take "precedence"?
Or does it take precedence, but just not have any impact?

Each evaluation of an expression has some result. Besides result each expression has possible side effect too. When we say that post fix operator has no impact on the variable it simply means that operator has no side effect till its complete evaluation .
so when we write some expression like the result of the expression is value of a stored and to evaluate the expression we need precedence and associativity. but its side effect is increment in value of a which happens when expression completes.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Simple answer: no.
The n++ operator does not have a side effect. As I said earlier it has two different results. It has an immediate effect on the variable but the whole expression n++ is not a variable but a value. That is equal to the old value of n.
It is all in the Java Language Specification, but that is by no means easy to understand.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!