How does postfix increment operator work with assignment?
Let's take a look the following code:
public class Program {
public static void main(String[] args) {
int i = 0;
for (int j = 0; j != 10; j++) {
i = i++;
}
System.out.println(i);
}
The output is "0". Why?
Let's take a look the Postfix increment Operator ++ definition in Java language Specification:
A postfix expression followed by a ++ operator is a postfix increment expression. The result of the postfix expression must be a variable of a type that is convertible to a numeric type, or a compile-time error occurs. The type of the postfix increment expression is the type of the variable. The result of the postfix increment expression is not a variable, but a value.
At run time, if evaluation of the operand expression completes abruptly, then the postfix increment expression completes abruptly for the same reason and no increment occurs. Otherwise, the value 1 is added to the value of the variable and the sum is stored back into the variable. Before the addition, binary numeric promotion is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion and/or subjected to boxing conversion to the type of the variable before it is stored. The value of the postfix increment expression is the value of the variable BEFORE the new value is stored.
Note that the binary numeric promotion mentioned above may include unboxing conversion and value set conversion. If necessary, value set conversion is applied to the sum prior to its being stored in the variable.
A variable that is declared final cannot be incremented (unless it is a definitely unassigned blank final variable, because when an access of such a final variable is used as an expression, the result is a value, not a variable. Thus, it cannot be used as the operand of a postfix increment operator.
Let's go back to our sample code. When the postfix increment expression is evaluated, the value of i (e.g., 0) is incremented, and new value that is i+1 (e.g., 1) is stored. The assignment is executed, its original value of the expression itself "0" (BEFORE the increment) is assigned into i.
Let's take look how it happens in the process of "i = i++;":
public void testPostfix() {
int i = 1;
i = i++;
}
A compiler might compile testPostfix() to the following Java bytecodes
public void testPostfix();
Code:
//Push int constant 1 onto the stack
0: iconst_1
//Pop constant 1 off the stack and store it in local variable 1 (i=1)
1: istore_1
//Push local variable 1 value (that is int 1) onto the stack
2: iload_1
//Increment local variable 1 by 1 (i++)
3: iinc 1, 1
//Pop top value that is pushed at line 2 off the stack and store
// it in local variable 1 (i=1)
6: istore_1
7: return
How to disassemble class files? Please reference to javap - The Java Class File Disassembler. The instructions are documented in the Java Virtual Machine Specification or you can visit Java Virtual Machine Online Instruction Reference.
Most Recent java Faqs
- How to avoid an java.util.ConcurrentModificationException with ArrayList?
- How to convert a given array to a list in Java?
- How to make Java objects eligible for garbage collection?
- What are local variables in Java?
- What are instance variables in Java?
- How many backslashes?
- What are class variables in Java?
Most Viewed java Faqs
- How to use HttpURLConnection POST data to web server?(24745)
- What is runtime polymorphism in Java?(18328)
- How to add BASIC Authentication into HttpURLConnection?(16088)
- What is String literal pool?(14754)
- Can the run() method be called directly to start a thread?(13991)
- What does Class.forname method do?(10593)
- Can transient variables be declared as 'final' or 'static'?(10446)