Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Can someone explain postfix operator? SCJP1.4

 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm taking the exam friday and still can't grasp this basic idea.
let's say we have a code snippet like:


at line 2 j will be 10, but i will be 11?
so you can assign a postoperand and get the preoperand value?
but the variable that uses the postoperand will still have to wait to get the real value?
when does the post operand variable get the new value? does it have to be called again or something?
 
lowercase baba
Posts: 13074
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i may have some of the finer points wrong, but basically i think of it this way... When you say i++, you're saying "figure out the value of i, rember that, then incremnt i (but don't forget that old value)".

so y = i++; becomes " well, i is 10 and i'll remember that. then i'll increment i to 11. I now need to assign the value i'm remembering to y, so y becomes 10."
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dilla,

"++" has higher precedence than "=", so j will get the new value after i has been incremented. But because of postfix operation in i, j will get the value in i before being incremented.

hope this helps.

Originally posted by Jay Dilla:
i'm taking the exam friday and still can't grasp this basic idea.
let's say we have a code snippet like:


at line 2 j will be 10, but i will be 11?
so you can assign a postoperand and get the preoperand value?
but the variable that uses the postoperand will still have to wait to get the real value?
when does the post operand variable get the new value? does it have to be called again or something?

 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look to these links:
Post Increment Operator And Assignment

Post-Increment Operator with Assignment
 
reply
    Bookmark Topic Watch Topic
  • New Topic