• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Precedence between postfix++ and ++prefix

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic