What is happening in the code below?
public static void main (String[] args) {
int x = 5;
x = x++;
System.out.println(x); // 5. So what happened to the ++?
}
I can understand x is assigned the value 5, because we told it to take the value before increment.
However, it must still do the increment, so is this done on a temporary value?
I don't understand Java bytecode but it dissassembles as follows.
public static void main(java.lang.String[]);
Code:
0: iconst_5
1: istore_1
2: iload_1
3: iinc 1, 1
6: istore_1
7: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
10: iload_1
11: invokevirtual #3 // Method java/io/PrintStream.println:(I)V
14: return
I can't see where it's creating a temp. What's really going on?