File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Precedence between postfix++ and ++prefix Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Precedence between postfix++ and ++prefix" Watch "Precedence between postfix++ and ++prefix" New topic

Precedence between postfix++ and ++prefix

Clement Hui

Joined: Mar 01, 2002
Posts: 2
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?
Alex Ku
Ranch Hand

Joined: Jan 15, 2002
Posts: 47
if i = 0, then
i++ + --i looks like this:

hope you could understand
[ March 01, 2002: Message edited by: kawaii desu ]
[ March 01, 2002: Message edited by: kawaii desu ]
[ March 01, 2002: Message edited by: kawaii desu ]
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
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:
[ March 01, 2002: Message edited by: Valentin Crettaz ]

[Blog] [Blogroll] [My Reviews] My Linked In
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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

Clement Hui

Joined: Mar 01, 2002
Posts: 2
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?
Alex Ku
Ranch Hand

Joined: Jan 15, 2002
Posts: 47
Here is the link from Java Tutorial on sun's website, Expressions, Statements, and Blocks

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?
[ March 01, 2002: Message edited by: kawaii desu ]
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9053
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.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
I agree. Here's the link:
subject: Precedence between postfix++ and ++prefix