preface

As is known to all, Java bytecode technology make Java “a deployment, run everywhere” great ideal, Java USES a custom in the middle of the language, not based on the underlying operating system interface, but based on his own VM virtual machine, the docking to deal with the underlying operating system to the VM virtual machine, Java developers don’t need to care about these, Even if you want to care, you can use the corresponding tool class or parameter configuration provided by Java. So what is bytecode?

concept

Java bytecode (English: Java Bytecode) is an instruction format executed by the Java virtual machine. Most opcodes are one byte long, and some operations require arguments, resulting in some multi-byte opcodes. And not all of the possible 256 opcodes are used; Fifty-one of the opcodes were reserved for future use. On top of that, sun Microsystems, the original Java platform developer, has kept three additional pieces of code permanently out of use.

In actual combat

First, define a simple base class. As follows:

public class SimpleAddDemo { public static void main(String[] args) { int a = 28; int b = 2; for (int i=0; i<7; ++i) { if (i % 2 == 0) { a++; b++; } } long c = (a + b) * 5; }}Copy the code

This is a simple numeric logic operation. Next, compile it using Javac. Decompile the class file using javap -C to get the following bytecode information

public class com.jackpan.jvm.basic.SimpleAddDemo {
  public com.jackpan.jvm.basic.SimpleAddDemo();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: bipush        28
       2: istore_1
       3: iconst_2
       4: istore_2
       5: iconst_0
       6: istore_3
       7: iload_3
       8: bipush        7
      10: if_icmpge     31
      13: iload_3
      14: iconst_2
      15: irem
      16: ifne          25
      19: iinc          1, 1
      22: iinc          2, 1
      25: iinc          3, 1
      28: goto          7
      31: iload_1
      32: iload_2
      33: iadd
      34: iconst_5
      35: imul
      36: i2l
      37: lstore_3
      38: return
}
Copy the code

Just focus on the Code section of the main method to explain these instructions: Bipush 28 When the value of int is between -128 and 127, the JVM uses bipush instructions to push the constant onto the stack. Istore_1 stores the top value of the stack into the second local variable, iconst_2, and pushes the value of int 2 onto the stack. The JVM uses the Iconst instruction to push constants onto the stack. Iload_3 pushes the fourth local int variable to the top of the stack if_icmpGE 30 Compares the values of the two ints at the top of the stack Ifne 25 When the int value at the top of the stack is not equal to 0, it jumps to 25 lines. Iinc increases the int variable at the top of the stack by a specified value. Goto 7 Jumps to 7 lines unconditionally Imul multiplies the top two ints and pushes the result to the top of the stack. I2l Forces the top int to a long and pushes the result to the top of the stack. Return Returns void from the current method

conclusion

The above 13 commands contain most of the numerical manipulation, and many of the commands are variations of one or two letters, which can be extended by a single command.

reference

Java bytecode – Wikipedia official JVM bytecode documentation