The moose likes Beginning Java and the fly likes post(not)incremented? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "post(not)incremented?" Watch "post(not)incremented?" New topic


Adrian Airloy

Joined: Dec 31, 2002
Posts: 11
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);
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.

SCJP.<br />I haven't lost my mind, it's backed up on a disk somewhere!
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
I agree. Here's the link:
subject: post(not)incremented?
It's not a secret anymore!