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.
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_infostructure (§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 theClass.getNamemethod, that is:-
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:
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. -
-
A symbolic reference to a field of a class or an interface is derived from a
CONSTANT_Fieldref_infostructure (§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. -
A symbolic reference to a method of a class is derived from a
CONSTANT_Methodref_infostructure (§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. -
A symbolic reference to a method of an interface is derived from a
CONSTANT_InterfaceMethodref_infostructure (§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. -
A symbolic reference to a method handle is derived from a
CONSTANT_MethodHandle_infostructure (§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. -
A symbolic reference to a method type is derived from a
CONSTANT_MethodType_infostructure (§4.4.9) in the binary representation of a class or interface. Such a reference gives a method descriptor (§4.3.3). -
A symbolic reference to a call site specifier is derived from a
CONSTANT_InvokeDynamic_infostructure (§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.
In addition, certain run-time values which are not symbolic references are derived from items found in the constant_pool table:
-
A string literal is a
referenceto an instance of classString, and is derived from aCONSTANT_String_infostructure (§4.4.3) in the binary representation of a class or interface. TheCONSTANT_String_infostructure 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 methodString.internis called on any string, the result is areferenceto the same class instance that would be returned if that string appeared as a literal. Thus, the following expression must have the valuetrue:("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_infostructure.- If the method
String.internhas previously been called on an instance of classStringcontaining a sequence of Unicode code points identical to that given by theCONSTANT_String_infostructure, then the result of string literal derivation is areferenceto that same instance of classString. - Otherwise, a new instance of class
Stringis created containing the sequence of Unicode code points given by theCONSTANT_String_infostructure; areferenceto that class instance is the result of string literal derivation. Finally, theinternmethod of the newStringinstance is invoked.
- If the method
-
Run-time constant values are derived from
CONSTANT_Integer_info,CONSTANT_Float_info,CONSTANT_Long_info, orCONSTANT_Double_infostructures (§4.4.4, §4.4.5) in the binary representation of a class or interface.Note that
CONSTANT_Float_infostructures represent values in IEEE 754 single format andCONSTANT_Double_infostructures 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.
The remaining structures in the constant_pool table of the binary representation of a class or interface - the CONSTANT_NameAndType_info and CONSTANT_Utf8_info structures (§4.4.6, §4.4.7) - are only used indirectly when deriving symbolic references to classes, interfaces, methods, fields, method types, and method handles, and when deriving string literals and call site specifiers.