| FazBrowse Wikipedia Viewer | Main Page | | |
| Viewing: https://en.wikipedia.org/wiki/Java_class |
| Contents |
|---|
| History |
| File layout and structure |
| Sections |
| Magic Number |
| General layout |
| Representation of a class file |
| Java class file | |
|---|---|
| Internet media type | application/java-vm, application/x-httpd-java, application/x-java, application/java, application/java-byte-code, application/x-java-class, application/x-java-vm |
| Developed by | Sun Microsystems |
A Java class file is a file (with the .class filename extension) containing Java bytecode that can be executed on the Java Virtual Machine (JVM). A Java class file is usually produced by a Java compiler from Java programming language source files (
On 11 December 2006, the class file format was modified under Java Specification Request (JSR) 202.
There are 10 basic sections to the Java class file structure:
Class files are identified by the following 4 byte header (in hexadecimal): CA FE BA BE (the first 4 entries in the table below). The history of this magic number was explained by James Gosling referring to a restaurant in Palo Alto:
"We used to go to lunch at a place called St Michael's Alley. According to local legend, in the deep dark past, the Grateful Dead used to perform there before they made it big. It was a pretty funky place that was definitely a Grateful Dead Kinda Place. When Jerry died, they even put up a little Buddhist-esque shrine. When we used to go there, we referred to the place as Cafe Dead. Somewhere along the line it was noticed that this was a HEX number. I was re-vamping some file format code and needed a couple of magic numbers: one for the persistent object file, and one for classes. I used CAFEDEAD for the object file format, and in grepping for 4 character hex words that fit after "CAFE" (it seemed to be a good theme) I hit on BABE and decided to use it. At that time, it didn't seem terribly important or destined to go anywhere but the trash-can of history. So CAFEBABE became the class file format, and CAFEDEAD was the persistent object format. But the persistent object facility went away, and along with it went the use of CAFEDEAD - it was eventually replaced by RMI."
Because the class file contains variable-sized items and does not also contain embedded file offsets (or pointers), it is typically parsed sequentially, from the first byte toward the end. At the lowest level the file format is described in terms of a few fundamental data types:
Some of these fundamental types are then re-interpreted as higher-level values (such as strings or floating-point numbers), depending on context. There is no enforcement of word alignment, and so no padding bytes are ever used. The overall layout of the class file is as shown in the following table.
| Byte offset | Size | Type or value | Description |
|---|---|---|---|
| 0 | 4 bytes | u1 = 0xCA hex |
magic number (CAFEBABE) used to identify file as conforming to the class file format |
| 1 | u1 = 0xFE hex | ||
| 2 | u1 = 0xBA hex | ||
| 3 | u1 = 0xBE hex | ||
| 4 | 2 bytes | u2 | minor version number of the class file format being used |
| 5 | |||
| 6 | 2 bytes | u2 | major version number of the class file format being used. Java SE 28 = 72 (0x48 hex), |
| 7 | |||
| 8 | 2 bytes | u2 | constant pool count, number of entries in the following constant pool table. This count is at least one greater than the actual number of entries; see following discussion. |
| 9 | |||
| 10 | cpsize (variable) | table | constant pool table, an array of variable-sized constant pool entries, containing items such as literal numbers, strings, and references to classes or methods. Indexed starting at 1, containing (constant pool count - 1) number of entries in total (see note). |
| ... | |||
| ... | |||
| ... | |||
| 10+cpsize | 2 bytes | u2 | access flags, a bitmask |
| 11+cpsize | |||
| 12+cpsize | 2 bytes | u2 | identifies this class, index into the constant pool to a "Class"-type entry |
| 13+cpsize | |||
| 14+cpsize | 2 bytes | u2 | identifies super class, index into the constant pool to a "Class"-type entry |
| 15+cpsize | |||
| 16+cpsize | 2 bytes | u2 | interface count, number of entries in the following interface table |
| 17+cpsize | |||
| 18+cpsize | isize (variable) | table | interface table: a variable-length array of constant pool indexes describing the interfaces implemented by this class |
| ... | |||
| ... | |||
| ... | |||
| 18+cpsize+isize | 2 bytes | u2 | field count, number of entries in the following field table |
| 19+cpsize+isize | |||
| 20+cpsize+isize | fsize (variable) | table | field table, variable length array of fields
each element is a field_info structure defined in https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.5 |
| ... | |||
| ... | |||
| ... | |||
| 20+cpsize+isize+fsize | 2 bytes | u2 | method count, number of entries in the following method table |
| 21+cpsize+isize+fsize | |||
| 22+cpsize+isize+fsize | msize (variable) | table | method table, variable length array of methods
each element is a method_info structure defined in https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.6 |
| ... | |||
| ... | |||
| ... | |||
| 22+cpsize+isize+fsize+msize | 2 bytes | u2 | attribute count, number of entries in the following attribute table |
| 23+cpsize+isize+fsize+msize | |||
| 24+cpsize+isize+fsize+msize | asize (variable) | table | attribute table, variable length array of attributes
each element is an attribute_info structure defined in https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7 |
| ... | |||
| ... | |||
| ... |
The following is a representation of a "JDK 10 Release Notes".
| Sun, Oracle | |
|---|---|
| Platform | |
| Major third-party |
| Conferences | |
|---|---|
| Organizations | |
| People |
| View original on Wikipedia | Back | FazBrowse Home |