Kenny Reid

Greenhorn

Posts: 4

posted 3 years ago

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.

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

posted 3 years ago

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

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

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.

Let's look at the Java Language specification. It says PostfixExpression ++, so the postfix operators do indeed associate to the left. But because

… you can only use a postfix operator once, so associativity does not apply.The result of the postfix increment expression is not a variable, but a value.

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.

posted 3 years ago

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

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

posted 3 years ago

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?

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.

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.

posted 3 years ago

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.

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

posted 3 years ago

Welcome to the Ranch

Simple answer: no.

The

It is all in the Java Language Specification, but that is by no means easy to understand.

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.