The next part of the class file is called the constant pool. Its purpose is to contain some constant values where they can be easily referenced elsewhere in the class file. The constant pool for this file follows. 000008 0020 32 constants 00000a 08001f 1. String #31 00000d 07001d 2. Class name #29 000010 070018 3. Class name #24 000013 07000e 4. Class name #14 000016 070013 5. Class name #19 000019 090002000a 6. Fieldref class #2 name-and-type #10 00001e 0a00040009 7. Methodref class #4 name-and-type #9 000023 0a0003000b 8. Methodref class #3 name-and-type #11 000028 0c000c0017 9. NameAndType name #12 descriptor #23 00002d 0c0016001c 10. NameAndType name #22 descriptor #28 000032 0c001b001e 11. NameAndType name #27 descriptor #30 000037 010007 12. UTF length=7 00003a 7072 696e 746c 6e println 000041 01000d 13. UTF length=13 000044 436f 6e73 7461 6e74 5661 6c75 65 ConstantValue 000051 010013 14. UTF length=19 000054 6a61 7661 2f69 6f2f 5072 696e 7453 7472 java/io/PrintStr 000064 6561 6d eam 000067 01000a 15. UTF length=10 00006a 4578 6365 7074 696f 6e73 Exceptions 000074 01000a 16. UTF length=10 000077 6865 6c6c 6f2e 6a61 7661 hello.java 000081 01000f 17. UTF length=15 000084 4c69 6e65 4e75 6d62 6572 5461 626c 65 LineNumberTable 000093 01000a 18. UTF length=10 000096 536f 7572 6365 4669 6c65 SourceFile 0000a0 010005 19. UTF length=5 0000a3 6865 6c6c 6f hello 0000a8 01000e 20. UTF length=14 0000ab 4c6f 6361 6c56 6172 6961 626c 6573 LocalVariables 0000b9 010004 21. UTF length=4 0000bc 436f 6465 Code 0000c0 010003 22. UTF length=3 0000c3 6f75 74 out 0000c6 010015 23. UTF length=21 0000c9 284c 6a61 7661 2f6c 616e 672f 5374 7269 (Ljava/lang/Stri 0000d9 6e67 3b29 56 ng;)V 0000de 010010 24. UTF length=16 0000e1 6a61 7661 2f6c 616e 672f 4f62 6a65 6374 java/lang/Object 0000f1 010004 25. UTF length=4 0000f4 6d61 696e main 0000f8 010016 26. UTF length=22 0000fb 285b 4c6a 6176 612f 6c61 6e67 2f53 7472 ([Ljava/lang/Stri 00010b 696e 673b 2956 ng;)V 000111 010006 27. UTF length=6 000114 3c69 6e69 743e <init> 00011a 010015 28. UTF length=21 00011d 4c6a 6176 612f 696f 2f50 7269 6e74 5374 Ljava/io/PrintSt 00012d 7265 616d 3b ream; 000132 010010 29. UTF length=16 000135 6a61 7661 2f6c 616e 672f 5379 7374 656d java/lang/System 000145 010003 30. UTF length=3 000148 282956 ()V 00014b 01000c 31. UTF length=12 00014e 4865 6c6c 6f2c 2077 6f72 6c64 Hello, world The constant pool of the class file begins with a two-byte unsigned number that indicates how many constants are used in the class. There are 32 constants in this class. However, there are only 31 constant entries in the file: constant 0 is reserved for the virtual machine's own use. The first constant in the file is numbered 1, the last 31. Each constant begins with a one-byte tag that tells what sort of constant is coming up. The tag defines the format of the bytes following it. There are 11 different tags, numbered from 1 to 12 (tag 2 is undefined); they are listed in Table 9.1. Constant pool entries often reference other constants, using a two-byte unsigned integer as the index into the constant pool. The DumpClass output displays such references by prefixing the index number (in decimal) with a "#." Order is not significant, and both forward and backward references are permitted. 9.2.1 UTF8 EntriesThe most common kind of constant is the UTF8 constant, represented by tag 1. UTF-8 stands for "UCS Transformation Format, 8 bits," which is a standard defined by the X/Open Company. It is a way of coding 16-bit Unicode strings so that they mimic 7-bit ASCII text. UTF8 constants are used to store arbitrary strings of information, and they are capable of storing arbitrary-length data. They're used by some of the other constants to hold variable-length information, like the values of strings and the names of methods. A UTF8 constant starts with two bytes that represent the length of the data, which may be up to 64 kilobytes. For example, in constant 31, the first four bytes are 000c, meaning that there are 12 bytes of data. The next 12 bytes are the actual data bytes. Each byte between 1 and 127 is interpreted as the ASCII value corresponding to that value. In this case, there are 12 bytes that are interpreted as the ASCII string "Hello, world." Non-ASCII characters are represented using two or three bytes having values greater than 127. The specifics of the code are defined in File System Safe UCS Transformation Format (FSS_UTF), X/Open Preliminary Specification, X/Open Company Ltd., Document number P316. The UTF8 standard is also documented in ISO/IEC 10646, Annex P. 9.2.2 Constant EntriesThe first constant in the constant pool is a String constant, identified by the tag byte 1. It's used to represent the value of string literals within the Java program. String constants are used in conjunction with ldc instructions and as field initializers. The value of a String constant is two bytes, taken together as a 16-bit unsigned integer referring to a UTF8 constant. The UTF8 constant is the value of the String. In constant 1, the two bytes are 001f, which points to constant 31. In hello.class, constant 31 is a UTF8 with the value Hello, world. Constants 3, 4, 5, and 6 represent the various kinds of numeric constants that can appear in Java programs. This file didn't contain any. Like String constants, these constants are used in conjunction with the ldc (push constant) instruction and with field initializers.
Integer and Long constants are written as two's complement signed integers, with 32 and 64 bits respectively, written from most significant byte to least significant byte. For example, suppose the program contained this line: long distance_to_sun = 149669000000; // Distance to sun in meters The compiler generates a Long constant. The eight bytes of this constant are: 0000 0022 D8F7 B340 Float and Double constants are also 32-and 64-bit values, but they are interpreted as representing floating-point numbers according to the IEEE 754 floating-point format (Table 9.2). The value is negative if the sign is 1, positive if the sign is 0. The exponent and mantissa are unsigned integers. For example, the line double pi = 3.14159265358979323; would cause the compiler to add a Double constant with the bytes: 4009 21FB 5444 2D18 The sign of this number is 0, since the leftmost bit is 0. The exponent is 400 in hex, or 1024 in decimal. The mantissa is 921FB54442D18, or 2,570,638,124,657,944 decimal. Plugging this into the formula for double, you get the value of pi. The String, Integer, and Float constants are usually the first in the constant pool. This allows them to have low numbers, which means that the short form of the ldc instruction can be used. If there are more than 255 of these constants in the constant pool, those with numbers 256 or above have to be used in conjunction with the ldc_w instruction, which takes up more space in the bytecodes. (Double and Long constrants require the ldc2_w instruction.)
9.2.3 Field, Method, and Class EntriesConstants with the tags 9, 10, or 11 have identical formats. They are used to refer to fields and methods in field and method instructions, such as getfield, putstatic, and invokevirtual. Constant 7 is an example of the Methodref constant. A Methodref constant contains two values that point to two other constants. The first points to a Class constant, which names the class containing the method. The second points to a NameAndType constant, which gives the name of the method and the method descriptor. Constant 7 points to constant 4 as the Class constant and constant 9 as the NameAndType constant. The Class constant in 4 contains a single piece of information: a reference to a UTF8 constant naming the class. In this case, that's the UTF8 constant 14: java/io/PrintStream. The NameAndType constant 9 contains the name and the type descriptor, each of which is a reference to a UTF8 constant. The values in this case are println and (Ljava/lang/String;)V. Putting all this together provides enough information to name the method to be called. Constant 7 could be used in the assembly of an instruction: invokevirtual java/io/PrintStream/println (Ljava/lang/String;)V In this instruction, the class is java/io/PrintStream, the method is println, and the descriptor is (Ljava/lang/String;)V. InterfaceMethodref and Fieldref constants are identical to Methodref constants. For an InterfaceMethodref, the Class entry refers to an interface instead of a class. For a Fieldref, the NameAndType entry points to a field instead of a method. |