6.2 Is It a Structurally Valid class File?
There are some ways in which the verification algorithm can tell right away that it's not going to like a file. The first four bytes must contain the hex values CA, FE, BA, BE. This magic number lets the verification algorithm immediately reject files that have been garbled and files that were never intended to be thought of as class files (like Java source files, Java archive files, and all the other files hanging around in the system).
Following the magic number are the minor version and the major version. Each takes up two bytes and is interpreted as a 16-bit unsigned value. For JDK 1.0 and 1.1, the major version number is 45 (0x2d) and the minor version number is 3. The Java 2 platform also accepts major version 46 (0x23) and minor version 0, as well as minor versions greater than 3 for version 45.
The Java 2 platform is backward compatible. If your class files are not meant to be read on a JVM prior to Java 2, then they may be marked with version 46. The Java 2 platform javac compiler supports this with the -target option; -target 1.2 will produce a class file with version 46.0, and -target 1.1 will produce a class file with version 45.3.
If all three numbers are acceptable, the verification algorithm scans the rest of the file to ensure that it has the correct format. Figure 6.1 depicts where the bytes belong in a properly formatted class file.
Figure 6.1. Is the file properly formatted?
Most sections begin with a count, which is a two-byte unsigned integer, followed by that many instances of some pattern of bytes. For example, following the major version number is the count of the number of constants. Each constant begins with a tag describing what sort of constant it is, which in turn tells how many bytes make up the constant. The set of constant tags is defined by the virtual machine specification. If any constant tag is invalid, or if the file ends before the correct number of constants are found, then the file is rejected. The valid constant tags are given in Table 6.1.
Similar rules apply to the other sections. If the file ends before all of the parts are found, or if there are extra bytes at the end, then the file is rejected. For more about the details of the inner workings of the class file, see chapter 9.