Deep understanding of Java Virtual Machine (3)---class structure

Source: Internet
Author: User

At the beginning of the computer, only 0 and 1 are known, so assembly language is tied to the machine structure or CPU. ARM architecture is one such embodiment, the concept of instruction set.

With the advent of high-level languages, from word coding to byte-coding, the pioneers of the computer wanted to be able to get the language out of the language, so programmers could develop code without the need for a device.

This is how the Java language was created. Java uses a middleware such as a virtual machine to communicate the connection between the language and the operating system, so that Java can be used across platforms. The design of the virtual machine does not

It is for Java only, but for other languages, it can provide platform-independent experiences in the future. So the virtual machine does not process ". Java" files, but ". Class" files.

class file Structure:

class file structure, is a standard format. The knowledge of class files is not our purpose. So this part is just necessary to understand it.

  

1.class Structure and magic number

The function of the magic number is to determine that the file format is the only file type that determines which virtual machines can accept.

The magic number of the class file is 0xCAFEBABY.

Then the version number: 5-6 bits is the minor version number, and the 7-8 bit is the major version number. The Java version starts from 45, java1.1 can support 45.0~45.65535 and java1.7 is 51.0.

In fact, the JDK comes with analysis tools JAVAP

JAVAP can analyze the structure of class content very well.

So specifically how to analyze class structure, this article will not introduce.

2. Constant pool

A constant pool holds 2 kinds of things, literal and symbolic references.

The ingress of the solid pool is the U2 type of data that represents the count value of the Chang's capacity.

This design is similar to the specifications in many computers.

Literal approach to the concept of Java, text strings and final constants.

Symbol References:

Fully qualified names for classes and interfaces

Field names and descriptors

The name and descriptor of the method

The Java class does not hold the final memory layout for each method, the field. That is, when the JVM is running, it needs to get the corresponding symbol reference from the constant pool.

At the time of class creation or runtime parsing, translate to a specific memory address.

Each constant of a constant pool is a table structure.

Let's look at a simple example:

How to define class information.

Look at this picture first, this is a typical class file:

First we see 0x00000032, so this Java version is 50.0 corresponds to the java1.6

The back 0x16 represents 22, which you need to note: The count of constant pools starts at 1, which is a total of 21 constant structures in a constant pool.

Look at the first 0x07, corresponding to: class type. More specific types, you can refer to the relevant book introduction. (The book will be covered later in this article)

In a nutshell, ClassInfo is 1 things, class name.

The position on the graph is 0x0002, which is the second constant that points to the constant pool.

The second constant pool flag is 0x01, which is a string constant.

The end result is the JAVAP analysis of the above section.

Its content is obviously the string of characters seen above. "Org/fenixsoft/clazz/testclass". This is the name of class.

Other constants can also be parsed by the type process.

JAVAP can help us analyze the constant pool directly.

3. Access Flags

The first time to think of is: public, private, protected. and a final,static.

However, this is a "class" qualification, so there will be other types of access flags.

Currently only 8 types are defined.

Can see: interface,enum, Annotations and other things, from the compiler's point of view, are similar.

4. Class index, parent class index, and interface index.

A class that has only one parent class and has 0~n interfaces.

So. The index of the class is the information of the current class, the parent class information, and the interface information.

These 3 indexes are linked together.

The first 2 represent the position in the constant pool, followed by the number of interfaces.

5. Byte code instruction

In the early years of the "computer composition and design hardware/software interface," the book is very enjoyable, the final flow of any high-level language is the instruction level, or CPU operation instructions.

And the computer essentially only knows 0 & 1, so simple can be understood as, after the Java language compiled, programming byte instructions, and then CPU processing.

So this section is not covered here.

Deep understanding of Java Virtual Machine (3)---class structure

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.