This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

A byte code

Bytecode is a cross-platform tool in Java. No matter how you write it, ultimately Java is going to execute bytecode, and bytecode is becoming more and more attractive to learners, such as multithreading, such as explaining efficiency problems.

View bytecode

JVM command: javap -c + class file to execute

  1. The implementation of i++
  • code

      int a =0;
      int i=0;
      a = i++;
    Copy the code
  • The bytecode

      4: iload_2
      5: iinc          2, 1
      8: istore_1
      
    Copy the code
  1. + + I perform
  • code

      int a =0;
      int i=0;
      a = ++i;
      
    Copy the code
  • The bytecode

    ++ I bytecode 4: iInc 2, 1 7: iload_2 8: istore_1Copy the code

Two bytecode execution analysis

  1. Explain i++

    First, the data in the second drawer of the local variable table is put on the stack, and then the second drawer of the local variable table is +1, and finally the data in the stack is put into the local variable table; The result is 0, and the difference is that we read the stack at the end, and the +1 is missing in the local table so it’s 0;

  2. Explain + + I

    Firstly, IINC performs +1 operation on the data in the local variable scale. (2,1) = (2,1) = (2,1) = (2,1) = (2,1) = (2,1) = (2,1) = (2,1) = (2,1) = (2,1) = (2,1) = (2,1) = (2,1) = (2,1) = (2,1) = (2,1) = (2,1)

    And then iloAD_2 is the second variable in the local variable table, and it goes on the stack, so it goes on the stack, and then istore_1 is the number at the top of the stack assigned to the local variable, so the local variable is 1

Three conclusion

In fact, there is very little code, but parsing still uses some JVM knowledge, if the knowledge is used to see, then it is difficult to understand the operation, but if their actual combat, not only can understand the meaning, but also can learn some other knowledge, why not?