`

JVM学习笔记-Class文件(Class File)

 
阅读更多

 

 

What is a Java Class File?

The Java class file is a precisely defined binary file format for Java programs. Each Java class file represents a complete description of one Java class or interface. There is no way to put more than one class or interface into a single class file. The precise definition of the class file format ensures that any Java class file can be loaded and correctly interpreted by any Java Virtual Machine, no matter what system produced the class file or what system hosts the virtual machine.

Java class文件是对Java程序二进制文件格式的精确定义。每一个Javaclass文件都对一个Java类或者Java接口做出了全面描述。一个class文件中只能包含一个类或者接口。无论Java class文件在何种系统上产生,无论虚拟机在何种系统上运行,对Java class文件的精确定义使得所有Java虚拟机都能够正确地读取和解释所有Javaclass文件。

 

Although the class file is related to the Java language architecturally, it is not inextricably linked to the Java language. As shown in Figure 6-1, you could write programs in other languages and compile them to class files, or you could compile your Java programs to a different binary file format. Nevertheless, most Java programmers will likely use the class file as the primary vehicle for delivering their programs to Java Virtual Machines.

尽管class文件与Java语言结构相关,但它并不一定必须与Java语言相关。如图6-1所示,可以使用其他语言来编写程序,然后将其编译为class文件,或者把Java程序编译为另一种不同的二进制文件格式。实际上,Javaclass文件的形式能够表示Java源代码中无法表达的有效程序,然而,绝大多数Java开发者几乎都会选择使用class文件作为程序传给虚拟机的首要方式。

As mentioned in earlier chapters, the Java class file is a binary stream of 8-bit bytes. Data items are stored sequentially in the class file with no padding between adjacent items. The lack of padding helps keep class files compact. Items that occupy more than one byte are split up into several consecutive bytes that appear in big-endian (higher bytes first) order.

如前所述,Java class文件是8位字节的二进制流。数据项按顺序存储在class文件中,相邻的项之间没有任何间隔,这样可以使class文件紧凑。占据多个字节空间的项按照高位在前的顺序分为几个连续的字节存放。

 

Just as your Java classes can contain varying numbers of fields, methods, method parameters, local variables, and so on, the Java class file can contain many items that vary in size or number from one class file to another. In the class file, the size or length of a variable-length item precedes the actual data for the item. This allows class file streams to be parsed from beginning to end, reading in the size of an item first followed by the item data.

和Java的类可以包含多个不同的字段,方法,方法参数,局部变量等一样,Java class文件也能够包含许多不同大小的项。在class文件中,可变长度项的大小和长度位于其实际数据之前。这个特性使得calss文件流可以从头到尾被顺序解析,首先读出项的大小,然后读出项的数据。

What's in a Class File?

The Java class file contains everything a Java Virtual Machine needs to know about one Java class or interface. The remainder of this chapter describes the class file format using tables. Each table has a name and shows an ordered list of items that can appear in a class file. Items appear in the table in the order in which they appear in the class file. Each item has a type, a name, and a count. The type is either a table name or one of the "primitive types" shown in Table 6-1. All values stored in items of type u2, u4, and u8 appear in the class file in big-endian order.

Java class文件中包含了Java虚拟机所知道的,关于类或者接口的所有信息。本章剩余部分将用表格形式描述class文件格式。每个表格都有一个名字,每个表格都显示了在class文件中出现的项的有序列表。这些项按照出现在class文件中的顺序在表中列出。每一项都包括类型、名称及该项的数量。类型或者未表名,或者为如表6-1所示的“基本类型”。所有存储在类型u2,u4和u8项中的值,在class文件中以高位在前的形式出现。

 

Table 6-1. Class file "primitive types"

u1 a single unsigned byte
u2 two unsigned bytes
u4 four unsigned bytes
u8 eight unsigned bytes

The major components of the class file, in their order of appearance in the class file, are shown in Table 6-2. Each of these components is described in more detail below.

可变长度的ClassFile表中的项,如表6-2所示,按照它们在class文件中出现的顺序列出了主要部分。

 

Table 6-2. Format of a ClassFile Table

Type Name Count
u4 magic 1
u2 minor_version 1
u2 major_version 1
u2 constant_pool_count 1
cp_info constant_pool constant_pool_count - 1
u2 access_flags 1
u2 this_class 1
u2 super_class 1
u2 interfaces_count 1
u2 interfaces interfaces_count
u2 fields_count 1
field_info fields fields_count
u2 methods_count 1
method_info methods methods_count
u2 attributes_count 1
attribute_info attributes attributes_count
  • 大小: 67.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics