Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

post(not)incremented?

 
Adrian Airloy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a bit baffled by the following.
public class Test
{
static int j;
public static void main(String argv[]){
for(int i =0 ; i<10; i++)
{
j = j++; //xx
System.out.println("i is :" + i);
System.out.println("j is :" + j);
System.out.println();
}
}
}
j gets its own value and the post increment has no effect on it as it does on i. After assigning the value of j to j, shouldnt it then increment j by 1? Changing line marked //xx to read 'j++;' increments j.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is the way the post-increment works. The behavior is clearly documented in the JLS. In C/C++ the behavior of this statement is undefined which means it may work one way in ove C implementation and another way in a different C implementation. It has been discussed in this forum quite a bit. Try a search.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic