We all know that Java programs run a piece of bytecode in JVM. What JVM needs to do is to convert these bytecode into machine languages so that Java programs can run correctly on computers, at the underlying level, the memory is allocated correctly, CPU computing is executed, and memory is released. Therefore, if any program can do the following: Read the Java class file, analyze the class file format, dynamically allocate memory for the variable object method, and manage the collection of these variables and memory, can be used as our so-called virtual machine to serve Java programmers. In this sense, JVM is like an intermediate conversion device between Java programs and machine languages.
To implement the conversion, the JVM is not in place in one step. The JAVA source code that programmers see is the closest to the human language for readability, so that we can easily design the functions and logic of the program. After compilation, these java files are converted into class files, all of which are bytecode. These bytecode are recognized by JVM, some are variable names, and some are variable values, some bytes are commands in the JVM instruction set, just like assembly languages. These bytecode are combined in a certain order, and a command is followed by a fixed number of operands. JVM also maintains some stack structures in the memory, and constantly pushes and pops the referenced addresses or values of basic type variables. By studying the JVM instruction set, we can help us understand the Java language, or sometimes help us analyze program performance.
First, write a simple Java program:
import java.util.*;public class Demo{ private static double d = 3.14; public static void main(String[] args){ List<String> list = new ArrayList<String>(); list.add("Hello"); int size = list.size(); String s = null; if(size>0) s = list.get(0); if(s!=null) s+=" World!"; System.out.println(d); System.out.println(s); }}
Some basic syntaxes are used here, such as interface signature, integer value assignment, String Structure, and string operation. Then we will execute javac demo. compile Java into demo. class file. JDK provides a decompilation instruction set command javap. Execute javap-C demo> instructions.txt to parse the class file, and the layout is for demo. the class file generates the JVM bytecode. Let's take a look at this bytecode:
Compiled from "Demo.java"public class Demo extends java.lang.Object{public Demo(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: returnpublic static void main(java.lang.String[]); Code: 0: new #2; //class java/util/ArrayList 3: dup 4: invokespecial #3; //Method java/util/ArrayList."<init>":()V 7: astore_1 8: aload_1 9: ldc #4; //String Hello 11: invokeinterface #5, 2; //InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z 16: pop 17: aload_1 18: invokeinterface #6, 1; //InterfaceMethod java/util/List.size:()I 23: istore_2 24: aconst_null 25: astore_3 26: iload_2 27: ifle 41 30: aload_1 31: iconst_0 32: invokeinterface #7, 2; //InterfaceMethod java/util/List.get:(I)Ljava/lang/Object; 37: checkcast #8; //class java/lang/String 40: astore_3 41: aload_3 42: ifnull 65 45: new #9; //class java/lang/StringBuilder 48: dup 49: invokespecial #10; //Method java/lang/StringBuilder."<init>":()V 52: aload_3 53: invokevirtual #11; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 56: ldc #12; //String World! 58: invokevirtual #11; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 61: invokevirtual #13; //Method java/lang/StringBuilder.toString:()Ljava/lang/String; 64: astore_3 65: getstatic #14; //Field java/lang/System.out:Ljava/io/PrintStream; 68: getstatic #15; //Field d:D 71: invokevirtual #16; //Method java/io/PrintStream.println:(D)V 74: getstatic #14; //Field java/lang/System.out:Ljava/io/PrintStream; 77: aload_3 78: invokevirtual #17; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 81: returnstatic {}; Code: 0: ldc2_w #18; //double 3.14d 3: putstatic #15; //Field d:D 6: return}
With this bytecode, we can get to know the JVM commands. Each Command has a corresponding bytecode, by the way, I will add a few parentheses after the instruction for your convenience. The first thing you can see is aload_0 (bytecode: 0x19 ), indicates reading a reference from the subscript 0 of the execution stack, followed by invokespecial (bytecode: 0xb7). Official explanation: invoke the method, special handling for superclass, private, and instance initialization method invocations. The difference between the following invokevirtual commands (bytecode: 0xb6) Is that invokevirtual is more common and calls an instance method, invokespecial is used in three scenarios:
1. Private Method
2. Call the inherited methods of the parent class
3. Initialization of each object
Therefore, the <init> action is used for initialization when an object is created. when an object is created in the JVM heap, once space is allocated in the heap, the <init> "method is called first, including the assignment of instance variables and initialization of static blocks. # Number indicates the subscript of each variable in the Operation stack. Other commands, such as dup, assign values to the entire operation stack. Pop pops up the top-level value of the operation stack. LDC refers to pushing the item from runtime constant pool, that is, pushing the value from the constant pool to the operation stack. Ifle and ifnull determine the branch statement, respectively, indicating whether it is less than or null, followed by the offset that the current pointer should jump to if the judgment is successful. With regard to offset, I believe that it is easier to understand C/C ++ or assembly.
Other commands will not be explained one by one. There is a small rule that operation commands of the int type generally start with I, such as float and double, array, Char, and short types. Here I made a special string type + = operation. Through The Decompilation command, we can see that the JVM contains "hello" and "world! "All come out from the constant pool (the meaning of the LDC command), and then the JVM constructs a stringbuilder (the early jdk1.4 should be a thread-safe but less efficient stringbuffer ), this class is used to implement the string connection + = operation. Each time + = is returned, a New String object. Therefore, we recommend that you directly use the stringbuilder class when operating a large number of strings. (Remember not to restrict more stringbuffer threads)
In some J2EE applications, familiarity with these commands can also help us penetrate into the framework. For example, the enhance proposed by JPA for object ing classes will directly change the bytecode of the class file, through decompilation, we can observe these commands to help us analyze the principle of modifying the class bytecode implemented by using dynamic proxies or the instrument interface? What is the performance?
For more information about JVM instruction sets, see the official documentation: http://java.sun.com/docs/books/vmspec/index.html.