• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Precedence between postfix++ and ++prefix

 
Clement Hui
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9068
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic