According to "A Programmer's Guide to Java Certification", the postfix++ operator has higher precedence than the ++prefix operator. So I would expect the result of "i++ + --i" will be identical to "--i + i++". But this is not the case. Could someone please explain to me what am I missing?
This has not to do with operator precedence but with how the i++ operation is carried out. The expression i++ + --i will evaluate to 2*i, while --i + i++ will always evaluate to 2*(i-1) Refer to the following thread for a better understanding of how ++ operator works: http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014986 [ March 01, 2002: Message edited by: Valentin Crettaz ]
The other part of this problem is that you are using the + (binary addition). In the JLS section 15.7.2 it says that all operands wil be fully evaluated before the operation is carried out, and further more that operands are evaluated left to right. In your first example 'i++ + --i', if i = 1: the i++ is evaluated first, for this expression it evaluates to 1 because it is a post fix operator. The value of 2 is stored into the i variable and the orginal value of i is used in the binary addition. Then the right hand operand is evaluated, the --i, this finds the value of 2 in the variable i, it decrements it to a 1 then it performs the binary addition by adding 1 + 1. In the second example, --i + i++, if i = 1: the first thing that happens is the --i is evaluated and the variable i is changed, in this case to 0, then the i++ is evaluated and the result is stored in i while the original value of i is used in the expression. So the expression becomes 0 + 0. hope this helps
Joined: Mar 01, 2002
Thx for the quick reply... but I am still confused. In the 2nd example: --i + i++; if i = 1 My interpretation is as follow: Step 1. postfix++ has the highest precedence among --i, + & i++, so it will be evaluated first (ie. the value of 1 is saved and i is incremented to 2). Step 2. Next is the --i part, so i will be decremented to 1 first, and then the value of 1 is again saved. Step 3. Finally the + operator will added 1 + 1. Shouldnt the result be 2? TIA
Operators with higher precedence are evaluated before operators with a relatively lower precedence. Operators on the same line have equal precedence. postfix operators:  . (params) expr++ expr-- unary operators : ++expr --expr +expr -expr ~ ! ... When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated in left-to-right order. Assignment operators are evaluated right to left.
However, when you evaluate the expression (--i + i++), the result does not agree with the above statements. --i is evaluated first, then i++. Do I miss something or the tutorial is wrong? postfix and unary have the same precedence? thanks, kawaii [ March 01, 2002: Message edited by: kawaii desu ]
According to Peter van der Linden in Just Java 2, pre-increment and pre-decrement have higher precedence than post-increment and post-decrement. Since he leads the team of kernal programmers at Sun Microsystems, I would say that chances are good that A Programmer's Guide to Java Certification's statement is incorrect.
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
subject: Precedence between postfix++ and ++prefix