For each type it loads, a Java Virtual Machine must store a constant pool. A constant pool is an ordered set of constants used by the type, including literals (string, integer, and floating point constants) and symbolic references to types, fields, and methods. Entries in the constant pool are referenced by index, much like the elements of an array. Because it holds symbolic references to all types, fields, and methods used by a type, the constant pool plays a central role in the dynamic linking of Java programs.
虚拟机必须为每个被装载的类型维护一个常量池。常量池就是该类型所用常量的一个有序集合,包括直接常量(String ,integer和floating point常量)和对其他类型、字段和方法的符号引用。池中的数据项就像数组一样是通过索引访问的。因为常量池存储了相应类型所用到的所有类型、字段和方法的符号引用,所以它在Java程序的动态连接中起着核心作用。
Following the magic and version numbers in the class file is the constant pool. As mentioned in Chapter 5, "The Java Virtual Machine," the constant pool contains the constants associated with the class or interface defined by the file. Constants such as literal strings, final variable values, class names, and method names are stored in the constant pool. The constant pool is organized as a list of entries. A count of the number of entries in the list, constant_pool_count, precedes the actual list, constant_pool.
在class文件中,魔数和版本号后面的是常量池。正如第5章中所述,常量池包含了与文件中类和接口相关的常量。常量池中存储了诸如文字字符串、final变量值、类名和方法名的常量。Java虚拟机把常量池组织为入口列表的形式。在实际列表constant_pool之前,是入口列表中的技术constant_pool_count。
Many entries in the constant pool refer to other entries in the constant pool, and many items that follow the constant pool in the class file refer back to entries in the constant pool. Throughout the class file, constant pool entries are referred to by the integer index that indicates their position in the constant_pool list. The first entry in the list has an index of one, the second has an index of two, and so on. Although there is no entry in the constant_pool list that has an index of zero, the missing zeroeth entry is included in the constant_pool_count. For example, if a constant_pool list includes fourteen entries (with indexes one through fourteen), the constant_pool_count would be fifteen.
常量池中的许多入口都指向其他的常量池入口,而且class文件中紧随着常量池的许多条目也会指向常量池中的入口。在整个class文件中,指示常量池入口在常量池列表中位置的整数索引都指向这些常量池入口。列表中的第一项索引值为1,第二项索引值为2,以此类推。尽管constant_pool列表中没有索引值为0的入口,但缺失的这一入口也被constant_pool_count计数在内。例如,当constant_pool中有14项(索引值从1到14)时,constant_pool_count的值为15.
Each constant pool entry starts with a one-byte tag that indicates the type of constant making its home at that position in the list. Once a Java Virtual Machine grabs and interprets this tag, it knows what to expect after the tag. Table 6-3 shows the names and values of the constant pool tags.
每个常量池入口都从一个长度为一个字节的标志开始,这个标志指出了列表中该位置的常量类型。一旦java虚拟机获取并解析这个标志,Java虚拟机就会知道在标志后的常量类型是什么,表6-3列出了所有常量池标志的名字和值。
Table 6-3. Constant pool tags
Entry Type | Tag Value | Description |
CONSTANT_Utf8 | 1 | A UTF-8 encoded Unicode string |
CONSTANT_Integer | 3 | An int literal value |
CONSTANT_Float | 4 | A float literal value |
CONSTANT_Long | 5 | A long literal value |
CONSTANT_Double | 6 | A double literal value |
CONSTANT_Class | 7 | A symbolic reference to a class or interface |
CONSTANT_String | 8 | A String literal value |
CONSTANT_Fieldref | 9 | A symbolic reference to a field |
CONSTANT_Methodref | 10 | A symbolic reference to a method declared in a class |
CONSTANT_InterfaceMethodref | 11 | A symbolic reference to a method declared in an interface |
CONSTANT_NameAndType | 12 | Part of a symbolic reference to a field or method |
For each tag shown in Table 6-3, there is a corresponding table. The name of the table is formed by appending "_info" to the tag name. For example, the table that corresponds to the CONSTANT_Class tag is called CONSTANT_Class_info. The CONSTANT_Utf8_info table stores a compressed form of Unicode strings. The tables for the various kinds of constant pool entries are described in detail later in this chapter.
表6-3中的每一个标志都有一个相对应的表,表名通过在标志后加上“_info”后缀来产生。例如,对应于CONSTANT_Class标志的表名为CONSTANT_Class_info,表名为CONSTANT_Utf8_info的表中存储着Unicode字符串的压缩形式。对英语各种不同常量池入口的表将在本章后面详细描述。
The constant pool plays an important role in the dynamic linking of Java programs. In addition to literal constant values, the constant pool contains the following kinds of symbolic references:
在动态链接的Java程序中,常量池充当了十分重要的角色。除了字面常量(或者说直接量)值以外,常量池还可以容纳下面几种符号引用。
A field is an instance or class variable of the class or interface. A field descriptor is a string that indicates the fieldís type. A method descriptor is a string that indicates the methodís return type and the number, order, and types of its parameters. The constant poolís fully qualified names and method and field descriptors are used at run time to link code in this class or interface with code and data in other classes and interfaces. The class file contains no information about the eventual memory layout of its components, so classes, fields, and methods cannot be referenced directly by the bytecodes in the class file. The Java Virtual Machine resolves the actual address of any referenced item at run time given a symbolic reference from the constant pool. For example, bytecode instructions that invoke a method give constant pool index of a symbolic reference to the method to invoke. This process of using the symbolic references in the constant pool is described in more detail in Chapter 8, "The Linking Model."
字段是类或接口的实例变量或者类变量。字段的描述符是一个指示字段的类型的字符串。方法的描述符也是一个字符串,该字符串指示方法的返回值和参数的数量,顺序和类型。在运行时,Java虚拟机使用常量池的全限定名,方法和字段的描述符,把当前类或接口中的代码与其他类或接口中的代码连接起来。由于class文件并不包含其内部组件最终内存布局的信息,因此类,字段和方法并不能被class文件中的字节码直接引用。Java虚拟机从常量池获得符号引用,然后在运行时解析引用项的实际地址。例如,用来调用方法的字节码指令把一个符号引用的常量池索引传给所调用的方法。在常量池中使用符号引用的过程在第8章有更详细的阐述。
----------------------------------------------------------------------------------------------------------------------------------
The constant pool is an ordered list of cp_info tables, each of which follows the general form shown in Table 6-8. The tag item of a cp_info table, an unsigned byte, indicates the tableís variety and format. cp_info tables come in eleven varieties, each of which is described in detail in the following sections.
Table 6-8. General form of a cp_info table
常量池是一个可变长度cp_info表的有序序列。这些cp_info表的通常形式如表6-8所示。Cp_info表中的tag(标志)项是一个无符号的byte类型值,它表明了表的类型和格式cp_info表一共有11中类型。
Type | Name | Count |
u1 | tag | 1 |
u1 | info | depends on tag value |