jQuery in Action, 2nd edition*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "post(not)incremented?" Watch "post(not)incremented?" New topic
Author

post(not)incremented?

Adrian Airloy
Greenhorn

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);
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.


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: http://aspose.com/file-tools
 
subject: post(not)incremented?
 
Similar Threads
post increment
Bowled over by a simple code
Basic java
Help with Timers!
value of i?