As long as Java knows the usage of the + + and-operator, as
- int i = 1;
- int j = i++;
- int k = ++i;
Results I was 3,j to 1,k for 3.
The following code:
- Int J = 0;
- for (int i = 0; i < ; i++) {
- J = j + +;
- }
- System.out.println (j);
How much does the output result? 100? 0?
The correct answer is 0. Why is it?
To figure this out, take a look at the bytecode generated by this code:
[Java]
- 0:iconst_0
- 1:istore_1
- 2:iconst_0
- 3:istore_2
- 4: goto
- 7:iload_1
- 8:iinc 1, 1
- 11:istore_1
- 12:iinc 2, 1
- 15:iload_2
- 16:bipush
- 18:IF_ICMPLT 7
- 21:getstatic #16; //field Java/lang/system.out:ljava/io/printstream;
- 24:iload_1
- 25:invokevirtual #22; //method java/io/printstream.println: (I) V
- : return
The byte code for j = j + + is:
[Java]
- 7:iload_1
- 8:iinc 1, 1
- 11:istore_1
Iload_1 means to take the variable of position 1 in the local variable table and put it in the operand stack;
Iinc 1, 1 This is the J + + operation, which adds 1 to the variable of position 1 in the local variable table;
Istore_1 This is the value that pops the top of the operand stack to the variable in the local variable table position 1.
The problem is innc this instruction, the format of the instruction is:
INNC Vindex Const
This instruction can only manipulate variables of type int, there are two operands, the first operand vindex indicates the positional index of the local variable in the local table, and the second operand, const, represents the integer constant to be added. Such as
INNC 2 100
Adds 100 to the variable of position 2 in the local variable table.
The specific process is to take the variable of position 2 in the local variable table, add 100, and then put it back into the local variable table.
The const value range is -128~127, and if this range is exceeded, the directive Iinc_w is used. Such as:
[Java]
- int x = 1;
- x = x +-128;
- x = x +-129;
- x = x + 127;
- x = x + 128;
The resulting bytecode is as follows:
[Java]
- 0:iconst_1
- 1:istore_1
- 2:iinc 1,- //x = x +-128;
- 5:iinc_w 1,-129 //x = x +-129;
- 11:iinc 1, 127 //x = x + 127;
- 14:iinc_w 1, //x = x + 128;
- : return
Above the basic meaning of the INNC directive is clear, and now is the most important point:
The INNC instruction operates on variables in the local variable table, not the data at the top of the current operand stack (the iinc instruction implementation has no use of the operand stack already unimportant).
The byte code above j = j + +;
[Java]
- 7:iload_1
- 8:iinc 1, 1
- 11:istore_1
Iload_1 the value of J first to put to the top of the stack, when the value is 0, the execution of Iinc 1, 1, the operation of the local variable table variable (the value is 0), add it 1, the value is 1,istore_1 instruction to the current operand stack top value (or 0), And put back in the variable of the local variable table position 1, the value of the variable of the local variable table position 1 is changed from 1 to 0.
So, no matter how the loop, the value of J is always 0.
Here are the questions about i++ and ++i:
All know that i++ first use the value of I, and then the value of I plus 1, and ++i is the first value of I plus 1, and then use the value of I.
But what's actually going on? In fact, i++ and ++i are used iinc Vindex, 1 instructions, the difference is that the i++ is to first take the local variable out to the top of the operand stack, and then the local variable table in the variable value plus 1, and ++i is the local variable table in the variable value 1, and then the local variable out to put the top of the operand stack. The following code:
[Java]
- Int J = 0;
- int x = j + +;
- x = ++j;
The byte code is as follows:
[Java]
- 0:iconst_0 //Put constant 0 on the top of the operand stack
- 1:istore_1 //int j = 0;
- 2:iload_1 //Remove the value of J first 0
- 3:iinc 1, 1 //j++, adds the value of J in the local variable table to 1,j=1, at which point the value of the top of the operand stack is 0 .
- 6:istore_2 //Put the value 0 on the top of the operand stack in the local variable table, at which time x=0
- 7:iinc 1, 1 //++j, first add the value of J in the local variable table by 1, at this time j=2
- 10:iload_1 //Remove the value of J 2
- 11:istore_2 //x=2
- : Return //method returns
The above is said to be the + + operator,--the operation is the same, no longer say more.
Java + + and--operators