Java class file structure

Source: Internet
Author: User

Note: Just read the "Deep Understanding Java Virtual Machine" class file This chapter, comb the idea.

Before that I only knew the source file. Java needs to be Javac compiled into a. class file first. So, how does this class file be saved? Read the book is a muddle-through do not understand, looked at three times probably know how to store it.

The original is to use the table and unsigned number, u1,u2,u4,u8, here you should be unsigned unsigned meaning, 1, 2, 4, 8 represents the byte, the role is used to describe the number AH, index reference Ah, quantity value or string and so on. Is that these two types form the Java class file.

Of course, it will not be stored in a disorderly, to find a good order to store.

1. Magic Number (Magic)

The magic number is a U4 type that simply indicates whether the file is acceptable to the virtual machine. Look at its specific value: Oxcaffbabe coffee, baby? Ha ha haha

2. Version number (Minor_version and major_version)

The 2nd, 3 represents the version of this class of files is good, is two U2,minor version of this release number, major version major release number java1.1 is starting from 45. Each large version responds to +1. The JVM can be backwards compatible, but it does not allow the lower version to run a high-version compiled class file, otherwise it will error: unsupported major version 50 (50 is java1.7).

3. Chang (Constant_pool_count and Constant_pool)

This constant pool is important and can be understood as a repository of resources in a class file, which is the data type that is most associated with other items in a class file and the type that consumes the most memory. As we said above, Java's class files are made up of unsigned numbers and tables. CONSTANT_POOL_COUNT constant counter, it is worth mentioning that it is starting from 1 count and our habits are different (usually starting from 0 count) The reason is that 0 means "there is no one constant pool item", the constant pool has 211 item constants.

Can be divided into two major categories: literal and symbolic references. The first literal is equivalent to Java's constant in Java, and the symbolic reference contains 3 classes: a) class and interface permission name b) field name and descriptor C) method name and descriptor.

Each item in a constant pool corresponds to a table

These 14 types correspond to 14 tables, each with its own corresponding data structure.

 

4. Access Flag (ACCESS_LAGS)

The end of Chang is immediately followed by the U2 access flag, which is used to identify some class or interface access information, such as (Acc_public is not a public type Ah, Acc_interface is not an interface AH) and so on

5. Class index, parent class index and Interface index collection (This_class, Super_class, Interface_count, interfaces)

It is important to note that, unlike the above, it is a collection. Imagine that the class we write can have only one parent class (except object) and multiple interfaces, how do we describe them? is to use this three data to determine the inheritance of this class. Note that all three data needs to use its permission name.  So what does Interface_count do? In a class file, all data that may contain multiple values has a corresponding count counter. Used as an entry point for finding this data.

6. Field table collection (Fields_count, fields)

As stated above, there is a corresponding count counter that may contain more than one data. So the effect of this fields_cont is also to provide entrance. Files is the type of a table that contains its corresponding field type. It is important to note that there is a difference between a simple name and a permission name. For example, sun.java.tools.mytools such a class with a permission name means: Sun/java/tools/mytools this "."  Replace with '/'. The simple name refers to a method or field that has no type and parameter adornments. For example, the method: void index (Char[]source,int count) is represented by a simple name: [CIV. Specific types of means Baidu a bit.

7. Collection of method Tables (Methods_count, methods)

Similar to the Set of field tables, the difference between the fields in the methods table is not quite the same.

8. Property sheet Collection

The collection of property sheets contains a large amount of data information, all of which have very strict order, length, and size. And the attribute table is not so strict, we write the most code is stored in the attribute table set in the Code table, a total of 21 items such as also contains: Exception table and so on. Each of the specific items is meaningful, a little more simple introduction to the main:

1) Code Property

After the code in the Java method body has been compiled by Javac, the bytecode instruction is eventually stored in the code attribute, and a code attribute appears in the property collection of the method table, but there is no code attribute in the interface or abstract class

2) Exception Property

Enumerate the exceptions that may be thrown in the method

3) Linenumbertable Property

Describes the correspondence between the Java source line number and the byte code line number (the byte code offset). The main thing is that if you throw an exception, the compiler displays the line number, which is the function of this property.

4) Localvariabletable Property

Describes the relationship between variables in a local variable table in a stack frame and variables defined in Java source code. The use of this method is to be able to display the parameter name of the method definition.

5) Soursefile Property

Record the name of the source file that generated this class file

can display the file name to which the error code belongs when throwing an exception

6) Constantvalue Property

Notifies the virtual machine to automatically assign a value to a static variable, only variables that are modified by the static character (class variable) can have this property

7) Innerclass Property

Used to record the association between the inner class and the host class

8, 9) deprecated and synthetic properties

Both of these are Boolean properties of the flag type

Deprecated means no longer recommended, and annotations are expressed as @deprecated

Synthetic indicates that this field or method is added by the compiler itself

  

Java class file structure

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.