my dog learned polymorphism*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Precedence between postfix++ and ++prefix" Watch "Precedence between postfix++ and ++prefix" New topic
Author

Precedence between postfix++ and ++prefix

Clement Hui
Greenhorn

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
kawaii
[ 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
Sheriff

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:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014986
[ March 01, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[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


Dave
Clement Hui
Greenhorn

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

Joined: Jul 22, 2000
Posts: 9043
    
  10
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Precedence between postfix++ and ++prefix
 
Similar Threads
Operator Precedence qstn.
increment operators
increment!
Operator precedence and postfix operators
yet another postfix/prefix precedence question