This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.

preface

  • No matter when learning C++ or Java, we were all irrigated by the teacheri++and++iThe different
  • The former is assigned before the +1 operation and the latter is assigned before the +1 operation. We are also sure that the improper use of both causes our program to miscalculate

Problem description

  • Here’s the thing! In line with the principle of adding before adding began to recite memory blindly. Always thought thati++++iThey execute differently. But then I looked at the bytecode
  • We do not need to elaborate on the specific scene here.

Problem analysis

  • We’re straight oni++,++iAnalyze. We need not dwell on the distinction. Today we will look at the difference between the two implementations at the bytecode level
  • Right! We need to look beyond the shell to the essence

  • This code should be pretty straightforward. Let’s take a look at his bytecode by compiling it. There are many ways to compile bytecode that you can go through directlyjavac. Here I use the idea plug-in to create. There are subtle differences but they don’t affect our reading. I’ve taken the bytecode from the main function for easy reading

  • Let’s understand the following instructions for the sake of understanding
instruction meaning
ICONST_x Push x onto the operand stack
ISTORE_x Writes the top element of the operand stack to position X +1 in the local table
IINC m n Add n to the m+1 position in the local variable table
LINENUMBER This is our line number
ILOAD_x Add position X +1 to the operand stack as opposed to ISTORE
  • With these five instructions in mind let’s read the bytecode of i++.
  • First push the value 3 onto the operand stack
  • Write the top element in the operand stack to the second position in the local variable table
  • ③ Add 1 to the second position of the local variable table
  • (4), end
  • ⑤ why is it added to the second position of the local variable scale? Because the first default of the local variable table is ARGS. That’s the main program

  • When I look at the++iAfter the bytecode is found withi++The execution is exactly the same. I ends up being 2 on the local variable scale. So understanding has to look at the bottom

Accustomed distinction

  • Above we show it at the bytecode leveli++++iThere is no difference between them. So what do we think of as differences
  • The ~. We have to look at it in conjunction with assignment to see what’s going on

  • Above are two pieces of code and their corresponding bytecodes. Internal is not with I ++ is first ILOAD then IINC.++ I is first IINC in ILOAD. As a result, the result of I in the local variable scale is different in the two programs

conclusion

  • Sometimes a summary of experience learned from others is just experience, and does not necessarily reveal something deep inside.
  • I++ is assigned plus 1. Many people would assume that two bytecodes are executed differently. Actually. You still have to look at the bottom

give a like