• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

A Question on Postfix Operator...

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have writeen the below code to test post fix operator.
public class OpsTest {
public static void main(String a[]) {
int i=2,j=2,k=3;
i=i++ + k;
j=j + k;
System.out.println("i= "+i+" j="+j);
}
}
I expected the value of i would be printed different from j. To my surprise I got both i and j values printed same. Can anyone explain me why the post fix operator was ignored in the first case.
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Sripada, and welcome to the ranch.
The trick is with this statement.

you might suspect that it is evaluated like this 'i = 3 + 3 ', but it is actually evaluated 'i = 2 + 3'. You ask why?

Hope this helps.
[ December 02, 2003: Message edited by: Vicken Karaoghlanian ]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sripada!
the postfix operator is not neglected,see the steps how's it executed
for the code i=i++ + k
i=2+3 ( now i is incremented to 3 ,so now i= 3)
i=5 ( now i is assigned the value of 5)
so finally i got the value of 5
bye
prasanthi
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic