post increment operator java
Posted
by srandpersonia
on Stack Overflow
See other posts from Stack Overflow
or by srandpersonia
Published on 2010-05-01T14:03:07Z
Indexed on
2010/05/01
14:07 UTC
Read the original article
Hit count: 187
I can't make heads or tails of the following code from "java puzzlers" by joshua bloch.
public class Test22{
public static void main(String args[]){
int j=0;
for(int i=0;i<100;i++){
j=j++;
}
System.out.println(j); //prints 0
int a=0,b=0;
a=b++;
System.out.println(a);
System.out.println(b); //prints 1
}
}
I can't get the part where j prints 0. According to the author,
j=j++
is similar to
temp=j;
j=j+1;
j=temp;
But
a=b++
makes b 1. So it should've evaluated like this,
a=b
b=b+1
By following the same logic, shouldn't
j=j++
be evaluated as,
j=j
j=j+1
Where does the temp come into picture here? Any explanations would be much appreciated. << I'm breaking my head over this. ;)>> Thanks in advance.
© Stack Overflow or respective owner