• 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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Can anyone explain this behaviour in detail.?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Trickytest {
public static void main ( String args [ ] ) {
int i = 10 ; // line 1
i = ++i ; // line 2
System.out.println("After 1st increment: "+i);
i = i++ ;
System.out.println("After 2nd increment: "+i);
i=i++;
System.out.println("After 3rd increment: "+i);
i=i++;
System.out.println("After 4th increment: "+i);
i=i++;
System.out.println("After 5th increment: "+i);

System.out.println ("value of "+ i ); // line 4
}
}

This prints value of i 11 each time ,i know i++ first assigns value and then increment i & ++i first assign and then increment i.But i need explanation because if i use j=i++;it prints incremented value..
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try removing "i=i++" to "i++" only
 
Mahesh Gurav
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got the explanation ,you can see it at..
web page
 
reply
    Bookmark Topic Watch Topic
  • New Topic