• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can anyone explain this behaviour in detail.?  RSS feed

 
Mahesh Gurav
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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..
 
Kartik Patel
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try removing "i=i++" to "i++" only
 
Mahesh Gurav
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got the explanation ,you can see it at..
web page
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!