• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

post(not)incremented?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic