Advanced Java: Detailed examples of Java virtual machine bytecode instructions

As we all know, the class file generated by the Java language compilation can run on any hardware platform and operating system that supports the Java virtual machine. So, have you thought about: What is included in the class file? How is it executed in a virtual machine? To clarify these problems, you must understand the class file structure and Java virtual machine bytecode instructions. Java virtual machine bytecode instructions are very important. Learning it is very useful to understand the principles of virtual machines, stacks, locks, exceptions, synchronization, etc. It is one of the must-read content on the road to advanced Java.

There are two articles in this series. This is the first one. The main contents are as follows:

Graphic interpretation of class file structure;

  • Introduction to Java bytecode;
  • Explain the stack structure of the Java virtual machine in detail;
  • Interpretation of variable and constant access principles and instruction system in Java virtual machine with examples;
  • Interpret the creation instructions of objects and arrays (numerical arrays, object arrays, multidimensional arrays) in the Java virtual machine by examples;
  • Interpretation of the principle and instruction system of method and field (also called domain-field) access in the Java virtual machine with examples.

Due to limited space, only part of it is shown here. Friends who need the full version and more related materials can click the link below to get it for free!

Link: 1103806531 Password: CSDN

Insert picture description here

introduction

The computer itself can only recognize the machine code composed of 0 and 1. Therefore, any programming language needs to be compiled into machine code before it can be executed by the computer. Taking C/C++ as an example, programs written with them are first compiled and then linked into a single binary file that supports a specific hardware platform and operating system. Normally, binary executable files on one platform cannot work on other platforms.

At the beginning of its birth, Java put forward the famous slogan: "Write Once, Run Anywhere". In order to achieve this goal, the Java Virtual Machine came into being. At present, there are many versions of the Java virtual machine, but they all have a common feature-they can load and execute the same platform-independent bytecode (ByteCode). Because of the emergence of the Java virtual machine, Java source code does not have to be compiled into 0 and 1 according to different platforms, but indirectly translated into bytecode, and the file storing the bytecode is then handed over to the Java virtual machine running on different platforms to read Execute, so as to achieve the purpose of writing once and running everywhere.

The bytecode file generated by Java compilation is a ".class" file, which is a binary file that contains the Java virtual machine instruction set, symbol table, and some other auxiliary information. As a general-purpose, machine-independent execution platform, implementers of any other language can use the Java Virtual Machine as a language product delivery medium. For example, using a Java compiler can compile Java code into class files that store bytecodes, and using compilers in other languages ​​such as Groovy, Scala, Koltin, etc. can compile program codes into class files. The virtual machine does not care about the source of the class. What language is it.

Class file structure

A class file is a set of binary streams based on 8-bit bytes. It uses a pseudo-structure similar to a C language structure to store data. This pseudo-structure has only two data types: unsigned numbers and tables. Unsigned numbers belong to the basic data types. U1, u2, u4, and u8 represent unsigned numbers of 1 byte, 2 bytes, 4 bytes, and 8 bytes respectively. Unsigned numbers can be used to describe A string value composed of numbers, index references, quantity values, or utf-8 encoding. A table is a composite data type composed of multiple unsigned numbers or other tables as data items, and all tables habitually end with _info. Each class file corresponds to a ClassFile structure as shown below:

ClassFile { u4 magic; //magic number u2 minor_version; //minor version number u2 major_version; //major version number u2 constant_pool_count; //constant pool counter, cp_info constant_pool[constant_pool_count-1]; //constant pool list u2 access_flags; //Access mark u2 this_class; //Class index, indicating the class or interface defined by this Class file u2 super_class; //Parent class index u2 interfaces_count; //Interface counter u2 interfaces[interfaces_count]; //Interface table, interface order Same as the source code sequence u2 fields_count; //field counter field_info fields[fields_count]; //field table u2 methods_count; //method counter method_info methods[methods_count]; //method table u2 attributes_count; //attribute counter attribute_info attributes[attributes_count ]; //Attribute table }
















More intuitively, show the structure of the class file through pictures, as shown below:

Insert picture description here

Class file structure example

From the above introduction, the structure of the class file is more complicated. In fact, we can divide the class file into the following parts.

Class file: file description, constant pool, class overview, field table, method table, and extended information table. In order to facilitate readers' understanding, here, let us first look at an example, the Java source code is as follows:

public class Test {
private String attribute_1;
private Integer attribute_2;
public void testMethod_1() {
} public String testMethod_2(String param) {
return param;
}}

After compiling with the command "javac Test.java", you can get the Test.class file, which is the so-called bytecode file. The contents of Test.class are as follows:

Insert picture description here

At last

Hope this article is helpful to everyone!

I have also compiled a complete set of video tutorials for architects and systematic materials on java, including java core knowledge points, interview topics, and the latest 20 years of Internet real questions and e-books.

Friends in need can click the link below to get it for free!

Link: 1103806531 Password: CSDN

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_48655626/article/details/108644894