Postfix Operators and Operator Precedence

I have a question about operator precedence. I don't understand how postfix operators can have the highest precedence yet act on the variable only after all other operations and the assignment have been completed.

It seems to me that the postfix increment and decrement operators should have the lowest precedence, below even the assignment operator, in order to explain their mode of operation.

Can someone clarify this for me?

Thanks,

Tom Blough

[479 byte] By [tblough] at [2007-9-27 19:57:26]
# 1
You are confused. Postfix simply removes the need for order of operations and () marksfor instance12+4*put 1 and 2 on the stack, pop them both and add, put 3 (their sum) back on the stack. Put 4 on the stack, multiply, put back on the stack
tjacobs01 at 2007-7-6 23:41:56 > top of Java-index,Archived Forums,Java Programming...
# 2

Sorry, I was questioning Java's handling of postfix operators and not RPN.

Example:

int a = 3;

int b = 4;

int c = 5;

int d;

d = b * c + a++;

By operator precedence the postfix increment has a precedence value of 1, the multiplication; 2, addition; 3, and the assignment; 14.

I understand that after the statement executes d = 23 and a = 4. However, the precedence rules would dictate that the postfix increment on "a" would be performed before any other operation in spite of the way postfix operators are supposed to work. I light of that, it seems that the precedence of postfix operators should be 15 and not 1.

Tom

tblough at 2007-7-6 23:41:56 > top of Java-index,Archived Forums,Java Programming...
# 3

Here are some results involving prefix ++, binary +, and postfix ++.

Answer me this... given the following code.

a = 5;

a = ++a + a++;

What is the result supposed to be?

My results (based on actual testing) are:

C says 13

C++ says 13

Perl says 13

Java says 12

Other languages not checked at this time.

Because of my results, I believe Java has a bug relating to the precedence.

nconantj at 2007-7-6 23:41:56 > top of Java-index,Archived Forums,Java Programming...
# 4

> a = 5;

> a = ++a + a++;

>

That's not an issue of precedence. All the languages you mentioned have the same precedence rules: the expression is evaluated as

a = ((++a) + (a++))

and not for example (++(a + (a++))) which wouldn't be possible anyway. The prefix and postfix operators must be applied to identifiers.

> Because of my results, I believe Java has a bug relating to the precedence.

The issue is more about evaluation order and the semantics of the post- and prefix operators. The java language specification clearly sets the evaluation order and semantics while the specification for C leaves it to the language implementor - with some other compiler for a different processor you can get a different result. I believe the C++ spec leaves this undefined too, I dont know about Perl. With different semantics you get different results.

jsalonen at 2007-7-6 23:41:56 > top of Java-index,Archived Forums,Java Programming...
# 5

int a = 3;

a = 5 + a++;

What is a's value now?

Well, the postincrementing does happen after all the other operations, thus:

a = 5+3;//a set to post-increment

but it happend before the assignment.

a = 8;//a has value of 3++ == 4

8 assigned to a, overwriting the 4.

phyzome at 2007-7-6 23:41:56 > top of Java-index,Archived Forums,Java Programming...
# 6

What a lot of pontifcatio here ...

Order of precedence=

++ , -- (Postfix) , ++ , -- (Prefix)

then: * , / , % , + , -

then bitwise stuff eg, << , >>> etc and then logical stuff eg, & , || etc, though it might be worth mentioning that bitwise AND '&' has a higher precedence that bitwise XOR '^' and bitwise OR '|' is lower down in the order of precedence that the other 2.

Note that java evaluates everything form left to right therefore

System.out.print("2+2=" + 2+2); // would result in;- '2+2=22'

and finally stuff on the left-hand side of the equals is evaluated AFTER everything on the right, therefore;

int i = 4;

i = 2 + 2 * i; // result i = 10 and not 16

i *= 2 + 2; // result i = 16 and not 10

LadyFoxy at 2007-7-6 23:41:56 > top of Java-index,Archived Forums,Java Programming...