JDK1.8 The Java® Virtual Machine Specification《Chapter 5. Loading, Linking, and Initializing》(1)

Chapter 5. Loading, Linking, and Initializing

章节5 虚拟机的加载,连接和初始化

The Java Virtual Machine dynamically loads, links and initializes classes and interfaces. Loading is the process of finding the binary representation of a class or interface type with a particular name and creating a class or interface from that binary representation. Linking is the process of taking a class or interface and combining it into the run-time state of the Java Virtual Machine so that it can be executed. Initialization of a class or interface consists of executing the class or interface initialization method <clinit> (§2.9).

Java虚拟机动态加载,连接和初始化类和接口。加载的目的是查找具有特定名称的二进制类或者接口,并且从这些二进制表现形式中创造一个类或者接口的过程。连接是获取类或者接口,并将其置为运行时状态的过程,目的是让其可以被Java虚拟机执行。初始化的要去执行类或接口的初始化方法。

5.1. The Run-Time Constant Pool

5.1运行时常量池

The Java Virtual Machine maintains a per-type constant pool (§2.5.5), a run-time data structure that serves many of the purposes of the symbol table of a conventional programming language implementation.

Java虚拟机维护了一个多类型的常量池,一个运行时数据结构来服务于传统编程语言的符号表的多种用途

The constant_pool table (§4.4) in the binary representation of a class or interface is used to construct the run-time constant pool upon class or interface creation (§5.3). All references in the run-time constant pool are initially symbolic. The symbolic references in the run-time constant pool are derived from structures in the binary representation of the class or interface as follows:

二进制形式表现的类或接口常量池表用来组装运行时常量池,所有的所有的运行时常量池中的引用都是初始化符号的形式。常量池中的符号引用来源于类或接口的二进制结构。如下所示

  • A symbolic reference to a class or interface is derived from a CONSTANT_Class_info structure (§4.4.1) in the binary representation of a class or interface. Such a reference gives the name of the class or interface in the form returned by the Class.getName method, that is:

  • 对类或者接口的符号化引用来源于二进制类或接口中的CONSTANT_Class_info ,这样的引用对应从类或接口的Class.getName方法中获得的返回值,即:
    • For a nonarray class or an interface, the name is the binary name (§4.2.1) of the class or interface.

    • 对于非数组类或接口,名称是类或接口的二进制名称

    • For an array class of n dimensions, the name begins with n occurrences of the ASCII "[" character followed by a representation of the element type:

      • If the element type is a primitive type, it is represented by the corresponding field descriptor (§4.3.2).

      • Otherwise, if the element type is a reference type, it is represented by the ASCII "L" character followed by the binary name (§4.2.1) of the element type followed by the ASCII ";" character.

    • 对于n维数组来说,这个名字一n个ASCII "["字符开头,后面紧跟着如下元素

      • 如果这个元素是基本类型,则他是相应的字段描述

      • 否则如果元素是引用类型,他将由ASCII "L"字符 + 元素类型的二进制名字 + ASCII ";"字符

  • Whenever this chapter refers to the name of a class or interface, it should be understood to be in the form returned by the Class.getNamemethod.

  • 无论这一章任何时候提及类或接口的名字,都应该理解为Class.getName方法的返回值

  • A symbolic reference to a field of a class or an interface is derived from a CONSTANT_Fieldref_info structure (§4.4.2) in the binary representation of a class or interface. Such a reference gives the name and descriptor of the field, as well as a symbolic reference to the class or interface in which the field is to be found.

  • 对类或者接口中字段的符号引用来源于类或接口的二进制CONSTANT_Fieldref_info结构,这样的引用提供了字段的名称和描述符,以及要被发现的字段在类或接口中的符号引用。(- -)

  • A symbolic reference to a method of a class is derived from a CONSTANT_Methodref_info structure (§4.4.2) in the binary representation of a class or interface. Such a reference gives the name and descriptor of the method, as well as a symbolic reference to the class in which the method is to be found.

  • 对类中方法的符号引用来源于类的二进制CONSTANT_Methodref_info 结构,这样的引用提供了方法的名称和描述符,以及要被发现的方法在类中的符号引用。

  • A symbolic reference to a method of an interface is derived from a CONSTANT_InterfaceMethodref_info structure (§4.4.2) in the binary representation of a class or interface. Such a reference gives the name and descriptor of the interface method, as well as a symbolic reference to the interface in which the method is to be found.

  • 对接口中方法的符号引用来源于接口的二进制CONSTANT_InterfaceMethodref_info 结构,这样的引用提供了接口方法的名称和描述符,以及要被发现的方法在接口中的符号引用。

  • A symbolic reference to a method handle is derived from a CONSTANT_MethodHandle_info structure (§4.4.8) in the binary representation of a class or interface. Such a reference gives a symbolic reference to a field of a class or interface, or a method of a class, or a method of an interface, depending on the kind of the method handle.

  • 对方法句柄中的符号引用来源于类或接口的二进制CONSTANT_MethodHandle_info 结构,这样的引用提供了类或接口字段的符号引用,或者类的方法或者接口的方法,依据方法句柄的类型。

  • A symbolic reference to a method type is derived from a CONSTANT_MethodType_info structure (§4.4.9) in the binary representation of a class or interface. Such a reference gives a method descriptor (§4.3.3).

  • 对方法类型的符号引用是从类或接口的二进制表示中的CONSTANT_MethodType_info 结构派生的。这样的引用给出了方法描述符。

  • A symbolic reference to a call site specifier is derived from a CONSTANT_InvokeDynamic_info structure (§4.4.10) in the binary representation of a class or interface. Such a reference gives:

    • a symbolic reference to a method handle, which will serve as a bootstrap method for an invokedynamic instruction (§invokedynamic);

    • a sequence of symbolic references (to classes, method types, and method handles), string literals, and run-time constant values which will serve as static arguments to a bootstrap method;

    • a method name and method descriptor.

  • 对调用点说明符的符号引用源于类或接口的二进制 CONSTANT_InvokeDynamic_info结构 。引用如下:

    • ​​​​​​​一个符号引用一个方法句柄,这将作为一个bootstrap 方法用于invokedynamic指令(§ invokedynamic);

    • 一系列符号引用(对类,方法类型和方法句柄),字符串文字和运行时常量值,它们将作为静态参数服务于bootstrap方法 ;

    • 方法名称和方法描述符。

In addition, certain run-time values which are not symbolic references are derived from items found in the constant_pool table:

此外,某些非符号引用的运行时值是从constant_pool表中找到的项派生的:

  • A string literal is a reference to an instance of class String, and is derived from a CONSTANT_String_info structure (§4.4.3) in the binary representation of a class or interface. The CONSTANT_String_info structure gives the sequence of Unicode code points constituting the string literal.

    The Java programming language requires that identical string literals (that is, literals that contain the same sequence of code points) must refer to the same instance of class String (JLS §3.10.5). In addition, if the method String.intern is called on any string, the result is a reference to the same class instance that would be returned if that string appeared as a literal. Thus, the following expression must have the value true:

    ("a" + "b" + "c").intern() == "abc"

    To derive a string literal, the Java Virtual Machine examines the sequence of code points given by the CONSTANT_String_info structure.

    • If the method String.intern has previously been called on an instance of class String containing a sequence of Unicode code points identical to that given by the CONSTANT_String_info structure, then the result of string literal derivation is a reference to that same instance of class String.

    • Otherwise, a new instance of class String is created containing the sequence of Unicode code points given by the CONSTANT_String_infostructure; a reference to that class instance is the result of string literal derivation. Finally, the intern method of the new String instance is invoked.

  • 一个String字符串是对一个String类的实例的引用。并且它源于类或接口二进制形式中的CONSTANT_String_info 结构。这个CONSTANT_String_info 结构提供了组成字符串的Unicode代码指针序列。java编程语言要求相同的字符串文字必须引用相同的字符串类实例(即包含相同编码指针的序列)。如果String.intern方法被任何String调用,他们的结果都将是一个人指向相同实例的引用类型,因此下面的值为true(intern() 方法返回字符串对象的规范化表示形式。

    ("a" + "b" + "c").intern() == "abc"

    如果对于一个类实例CONSTANT_String_info 结构中的有序Unicode编码指针序列String字符串已经预先调用过String.intern方法,那么接下来返回结果就是这个字符串实例的引用。否则将会创建一个新的字符串实例,它将存在于CONSTANT_String_info结构中,由一串有序Unicode编码组成。最后新字符串的intern方法将会被调用。

  • Run-time constant values are derived from CONSTANT_Integer_infoCONSTANT_Float_infoCONSTANT_Long_info, or CONSTANT_Double_infostructures (§4.4.4§4.4.5) in the binary representation of a class or interface.

    Note that CONSTANT_Float_info structures represent values in IEEE 754 single format and CONSTANT_Double_info structures represent values in IEEE 754 double format (§4.4.4§4.4.5). The run-time constant values derived from these structures must thus be values that can be represented using IEEE 754 single and double formats, respectively.

  • 运行时常量数值来自CONSTANT_Integer_infoCONSTANT_Float_infoCONSTANT_Long_info, or CONSTANT_Double_info结构中。注意 CONSTANT_Float_info的表示值遵循IEEE 754单精度格式的值的格式,CONSTANT_Double_info遵循IEEE 754双精度表示的值。因此运行时常量池中的值可以分别用单精度和双精度格式。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值