Java + + and--operators

Source: Internet
Author: User

As long as Java knows the usage of the + + and-operator, as

    1. int i = 1;
    2. int j = i++;
    3. int k = ++i;

Results I was 3,j to 1,k for 3.

The following code:

    1. Int J = 0;
    2. for (int i = 0; i < ; i++) {
    3. J = j + +;
    4. }
    5. 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]
  1. 0:iconst_0
  2. 1:istore_1
  3. 2:iconst_0
  4. 3:istore_2
  5. 4: goto
  6. 7:iload_1
  7. 8:iinc 1, 1
  8. 11:istore_1
  9. 12:iinc 2, 1
  10. 15:iload_2
  11. 16:bipush
  12. 18:IF_ICMPLT 7
  13. 21:getstatic #16;  //field Java/lang/system.out:ljava/io/printstream;
  14. 24:iload_1
  15. 25:invokevirtual #22; //method java/io/printstream.println: (I) V
  16. : return

The byte code for j = j + + is:

[Java]
    1. 7:iload_1
    2. 8:iinc 1, 1
    3. 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]
    1. int x = 1;
    2. x = x +-128;
    3. x = x +-129;
    4. x = x + 127;
    5. x = x + 128;

The resulting bytecode is as follows:

[Java]
  1. 0:iconst_1
  2. 1:istore_1
  3. 2:iinc 1,- //x = x +-128;
  4. 5:iinc_w 1,-129 //x = x +-129;
  5. 11:iinc 1, 127 //x = x + 127;
  6. 14:iinc_w 1, //x = x + 128;
  7. : 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]
    1. 7:iload_1
    2. 8:iinc 1, 1
    3. 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]
    1. Int J = 0;
    2. int x = j + +;
    3. x = ++j;

The byte code is as follows:

[Java]
  1. 0:iconst_0 //Put constant 0 on the top of the operand stack
  2. 1:istore_1 //int j = 0;
  3. 2:iload_1 //Remove the value of J first 0
  4. 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 .
  5. 6:istore_2 //Put the value 0 on the top of the operand stack in the local variable table, at which time x=0
  6. 7:iinc 1, 1 //++j, first add the value of J in the local variable table by 1, at this time j=2
  7. 10:iload_1 //Remove the value of J 2
  8. 11:istore_2 //x=2
  9. : Return //method returns


The above is said to be the + + operator,--the operation is the same, no longer say more.

Java + + and--operators

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.