• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reg. PostFix and PreFix Operators

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I always get kind of confused about Prefix and Postfix operators.
Fine i know what they actually mean but within an expression
i am always making the wrong guess.
Does someone have examples or possible conditions to be taken
care of while evaluating or shall i say "fixing" them !
 
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
Hi Angela
There are some goog explanations here
Let know if you need more
Val
 
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Valentin is right...
specially post by Usha Damarla in that thread is worth reading/understanding once. I too beleive that these operators work that way.
(by the way, that thread was also started by you (Angela), didn't you follow that thread or still something is confusing ?).

Originally posted by Valentin Crettaz:
Hi Angela
There are some goog explanations here
Let know if you need more
Val


 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this link
http://www.javaranch.com/ubb/Forum24/HTML/008856.html
and this
http://www.javaranch.com/ubb/Forum24/HTML/008873.html

HTH
tvs sundaram
SCJP
[This message has been edited by tvs sundaram (edited September 20, 2001).]
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the links. They have some real good info.
However i am still not very much clear about postfix operation
when the LHS and the RHS are the same variables.
For eg :

Value of i= 0
The postfix operator has to first use the value in the expression
and then increment. Right.
So the LHS value of 0 gets assigned to RHS value of i.
Then how to proceed...
Can you explain it in slightly simpler terms.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Angela,
The postfix operator has a higher precedence than the assignment operator so something similar to the following happens:
<list>

  1. take the initial value of 'i' in 'i++' (which is 0 ) and store it temporarily in memory
  2. increment 'i' in 'i++', which gives 1
  3. assign the initial value of 'i' which is 0

  4. </list>
    The result is that 'i' is now 0, the '1' resulting from the postfix operation is overwritten.
    Hope that helps.
    Also, check out Maha's
    simple and easy method explained in this older post

    ------------------
    Jane Griscti
    Sun Certified Programmer for the Java� 2 Platform
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jane, i think i finally understood it.
 
Amit Agrawal
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks jane for that link...maha has really done a good job.
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I just wanted to add one more thing as per the link
suggested by Jane. It has an excellent explanation for postfix
and prefix operators.
See the below code :

Output :
Value of i 12
Value of j 21
If the LHS and the RHS variables are different, then
1. The LHS ( j in the above case )always takes the value of the calculated non-bracketed
value of RHS ( ix in the above case )
2. The value of the RHS( i in the above case ) variable will be the last bracketed value.

Pls. correct me if wrong
[This message has been edited by Angela Narain (edited September 23, 2001).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic