Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is a Class file for getting started with JVM

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what is a Class file for JVM entry". In daily operation, I believe many people have doubts about what is a Class file for JVM entry. Xiaobian consulted all kinds of information and sorted out simple and easy to use operation methods. I hope to help you answer the doubts about "what is a Class file for JVM entry"! Next, please follow the small series to learn together!

What is a JVM Virtual Machine 1.1 Concept

Java as a programming language can be so widely recognized, in addition to its well-structured, object-oriented programming language, it also has a very prominent feature: once written, run everywhere, that is, written programs can get rid of hardware platform constraints, it provides a relatively safe memory management and access mechanism, avoiding most of the memory leak and pointer out of bounds problems.

When it comes to jvm, it is inseparable from the comparison with jdk and jre, so what is the difference and connection between them?

1.2 jdk/jre/jvm

Let's start with an architecture diagram like this,

From the perspective of set relations, jdk>jre>jvm, in addition to the difference in scope, we should understand the difference in the functions they contain and the roles they play.

jdk

The full name of jdk is Java Development Kit (java development kit), we can put the programming language, java virtual machine, java class library these three parts collectively referred to as jdk, jdk is used to support java program development minimum environment. Developer can easily use the methods in it to reduce the amount of code, which includes both jre and some development widgets (such as the compiler javac), which also include jre.

jre

The full name of jre is Java Running Environment (java runtime environment). The API subset of java SE in java class library API and java virtual machine can be collectively referred to as JRE. JRE is a standard environment that supports java program operation.

jvm

Jvm is the full name of java virtual machine (java virtual machine), it only knows XXX.class file, virtual machine can recognize the byte code instructions of this file and call the API on the operating system, it is for this reason, java can be used across platforms.

1.3 the execution of the code

After all, jvm is software, so how does it shield the details of the underlying operating system, hardware, and CPU instruction layer? Let's take a Java program as an example to analyze its execution process.

The basis for language independence is the storage format of virtual machines and bytecodes. Java virtual machines are not bound to any programming language, including Java. They are only associated with a specific binary file format, the Class file.

2. Class file structure

Class files are the key to good compatibility of the Java language, so what is the structure of the Class file and what does it store?

In fact, the Class file is a set of binary streams based on 8 bytes. Each data item is arranged in a compact order in the file without adding any separator in the middle, which makes the entire Class file store almost all the necessary data for the program to run. There is no gap.

The Java Virtual Machine Specification specifies that the Class file format uses a pseudo-structure similar to the C language structure to store data. This pseudo-structure contains only two data types, namely unsigned numbers and tables.

unsigned number

unsigned number is a primitive data type that can be used to describe numbers, index references, quantitative values, or string values encoded in UTF-8

table

A table is a composite data type consisting of multiple unsigned numbers or other tables as data items. For ease of distinction, all table names end with_info.

Class files are arranged in a fixed order of data structures and each data structure specifies a byte length to compact into a complete readable file. Jvm only needs to read step by step from the beginning of the file to fully parse the contents of the class file.

Take a look at what bytecode files look like!

In class files, the first four bytes are called magic numbers, which uniquely determine whether a class file is acceptable to a virtual machine. In fact, magic numbers are also widely used in GIF, JPEG and other file headers.

The next four bytes of the magic number store the version number of the Class file, bytes 5 and 6 are minor version numbers, and bytes 7 and 8 are major version numbers. Java version number is starting from 45, JDK 1.1 after each major version of the release of the major version number plus 1(JDK 1.0 ~1.1 used the version number of 45.0~45.3),"Java Virtual Machine Specification" in the Class file verification section explicitly requires that even if the file format has not changed, the virtual machine must also refuse to execute more than its version number of the Class file, so the higher version can be backward compatible with the previous version of the Class file, but can not run later versions of the JDK Class file.

2.2 constant pool

After the magic number and version number, the next location is the constant pool. The constant pool can be considered as the resource repository in the Class file. It is the data most associated with other projects in the Class file structure. The position occupied by the first two bytes of the constant pool is called the constant pool counter (constant_pool_cont), which records the number of constant pool entries (cp_info) that are components of the constant pool.

The constant pool counter starts from 1, not 0, i.e. if constant_pool_count=22, then the number of cp_info following is 21, because when specifying the class file specification, the zeroth constant is left empty to satisfy the requirement that certain data pointing to the index value of the constant pool express "do not reference any constant pool item" in certain cases, in which case the index value can be set to 0 to express.

There are two main types of constants stored in the constant pool: literals and symbolic references. Literals can be understood as constant concepts at the Java language level, such as text strings and constant values declared as final. Symbolic references include fully qualified names of classes and structures, names and descriptors of fields, names and descriptors of methods, and so on.

Class file stores all kinds of class information such as methods and fields, but it only stores it, and it cannot reflect the layout of methods, fields and other information in memory. This is because the Java language does not have the concept of linking like C++, but the Java language will make dynamic connections when the virtual machine is loaded, and the virtual machine will obtain the corresponding symbol references from the constant pool, and then parse and translate them to specific memory addresses at class creation or runtime.

2.3 access flag

After the end of the constant pool, the next two bytes represent access_flags, which are used to identify access information at the class or interface level. For example, identify whether a Class is a class or an interface; whether it is defined as a public type; whether it is defined as an abstract type; whether it is declared as final.

The corresponding relationship between mark value and mark name is as follows:

Flag Value Flag Name 0x0001ACC_PUBLIC0x0010ACC_FINAL0x0020ACC_SUPER0x0100ACC_INTERFACE0x0200ACC_ABSTRACT0x1000ACC_SYNTHETIC0x2000ACC_ANNOTATION0x4000ACC_ENUM

Flag name is to limit access information, such as ACC_PUBLIC indicates whether it is public type, ACC_FINAL indicates whether it is declared final, and other flags are similar.

2.4 index

After the end of the access flag, it is followed by the index, including the class index, parent class index and interface index set. The Class file can determine the inheritance relationship of the type by these three items of data. Let's first understand what each of these three types of indexes does

class index

The class index is used to determine the fully qualified name of the class, and the class is found by the fully qualified name of the class, so the role of the class index is to find out what the name of the class described in the class file is.

parent index

Parent class index is used to determine the fully qualified name of the parent class of this class. Java language does not support multiple inheritance, so except Object, there is only one parent class index for other classes.

Collection of interface indexes

It is used to describe which interfaces this class implements. Since interfaces are multi-implemented, these implemented interfaces will be arranged in order in the index set. A collection of interface indexes has a counter at the entry that indicates the number of indexes in the collection, or 0 if the class does not implement an interface.

Note: The class index, parent class index, and interface index collections point to symbol references in the constant pool.

2.5 Field Table Sets

Field table set is used to describe the variables declared in the interface or class. It consists of several field tables. The field table set is similar to an array structure. When jvm compiles the class, it counts the number of fields defined in the class into the field counter, and then puts each field information in the form of a structure after the field counter. the junction

In particular, the fields here include class variables and instance variables, but do not include declared local variables inside the method.

We're thinking about the question, what information does a field table store, what information is that, in fact, field tables store field information, and we organize it as follows

Modifier (public, protected, private)

Instance variables or class variables (static)

variability (final)

concurrent visibility (volatile)

Whether it can be serialized (transient)

Field data type (primitive type, object, array)

field name

Since the field has so much information, what is the form of its storage? In fact, the storage of fields is the same as the way we write fields. Don't you understand? Then let's look back!

In bytecode, JVM defines the filed_info structure to describe the field. Its form is also very simple. It is a structure.

Field_info{ access_flags; name_index; descriptor_index; attribute_count; attributes;}

access_flags is an access flag, similar in function to the access flags explained above, followed by name_index and descriptor_index, which are references to the constant pool, representing the simple name of the field and the descriptor of the field and method respectively. Simple names refer to method or field names without type and parameter modifiers; field and method descriptors refer to the first capital letter of the base type, such as byte, the method descriptor modifier is B. attribute_count represents an attribute counter, attributes contains three parts (attribute name index, attribute length, and constant value index)

2.6 method table collection

The structure of method table set is the same as that of field table set. We mainly explain the difference between them here, and the rest can be learned according to attribute table set.

distinguish one

For methods, the volatile and transient keywords do not modify methods, so there will be no corresponding flags in the access flags; but synchronized, native, stricftp, and abstract keywords can modify methods, so there will be corresponding access flags.

Difference 2

There is code inside methods compared to fields, so where is the code stored inside methods? In fact, Java code in a method, compiled into bytecode instructions by the Javac compiler, will have an attribute named Code stored in the method property table collection.

summary

The main structure of the Class file is finished. Let's take a look at what the Class file looks like from a macro perspective. Without saying much, let's look at the picture.

At this point, the study of "What is a Class file for JVM entry" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report