Content deleted Content added
m Fixing completely bare references Wikipedia:Bare_URLs |
→Representation in a C-like programming language: use more Java-like layout |
||
(25 intermediate revisions by 23 users not shown) | |||
Line 1:
{{Short description|Executable Java file format}}
{{
{{Infobox file format
| name = Java class file
Line 22 ⟶ 23:
}}
A '''Java class file''' is a [[Computer file|file]] (with the {{mono|.class}} [[filename extension]]) containing [[Java bytecode]] that can be executed on the [[Java
JVMs are available for many [[platform (computing)|platform]]s, and a class file compiled on one platform will execute on a JVM of another platform. This makes Java applications [[cross-platform|platform-independent]].
Line 33 ⟶ 34:
===Sections===
There are 10 basic sections to the Java class file structure:
* '''[[Magic number (programming)|Magic Number]]''': <code>0xCAFEBABE</code>
* '''Version of Class File Format''': the minor and major versions of the class file
* '''Constant Pool''': Pool of constants for the class
Line 41 ⟶ 42:
* '''[[Interface (object-oriented programming)|Interfaces]]''': Any interfaces in the class
* '''Fields''': Any fields in the class
* '''[[Method (
* '''Attributes''': Any attributes of the class (for example the name of the sourcefile, etc.)
Line 48 ⟶ 49:
<blockquote>
"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 Garcia|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 number (programming)|magic numbers]]: one for the persistent object file, and one for classes. I used CAFEDEAD for the object file format, and in [[grep]]ping 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 [[Java remote method invocation|RMI]]."
</blockquote>
Line 65 ⟶ 66:
{| class="wikitable"
|-
!
!
!
! Description
|-
| 0
Line 94 ⟶ 95:
| rowspan="2" | 2 bytes
| rowspan="2" | u2
| rowspan="2" | major version number of the class file format being used.<ref>{{Cite web|url=https://docs.oracle.com/javase/specs/jvms/
Java SE 25 = 69 (0x45 hex),<br />
Java SE 24 = 68 (0x44 hex),<br />
Java SE 23 = 67 (0x43 hex),<br />
Java SE 22 = 66 (0x42 hex),<br />
Java SE 21 = 65 (0x41 hex),<br />
Java SE 20 = 64 (0x40 hex),<br />
Java SE 19 = 63 (0x3F hex),<br />
Java SE 18 = 62 (0x3E hex),<br />
Java SE 17 = 61 (0x3D hex),<br />
Java SE 16 = 60 (0x3C hex),<br />
Line 223 ⟶ 232:
|}
===Representation
The following is a representation of a {{mono|.class}} file as if it were a C-style struct.
<syntaxhighlight lang="
struct
u4
u2
u2
u2
ConstantPoolInfo[constantPoolCount - 1] constantPool;
u2
u2
u2
u2
u2[interfacesCount] interfaces;
u2
FieldInfo[fieldsCount] fields;
u2
MethodInfo[methodsCount] methods;
u2
AttributeInfo[attributesCount] attributes;
}
</syntaxhighlight>
Line 260 ⟶ 269:
The constant pool table is where most of the literal constant values are stored. This includes values such as numbers of all sorts, strings, identifier names, references to classes and methods, and type descriptors. All indexes, or references, to specific constants in the constant pool table are given by 16-bit (type u2) numbers, where index value 1 refers to the first constant in the table (index value 0 is invalid).
Due to historic choices made during the file format development, the number of constants in the constant pool table is not actually the same as the constant pool count which precedes the table. First, the table is indexed starting at 1 (rather than 0), but the count should actually be interpreted as the maximum index plus one.<ref name="jvms-4.4">{{Cite web|url=https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-4.html#jvms-4.4|title=Chapter 4. The class File Format}}</ref> Additionally, two types of constants (longs and doubles) take up two consecutive slots in the table, although the second such slot is a phantom index that is never directly used.
The type of each item (constant) in the constant pool is identified by an initial byte ''tag''. The number of bytes following this tag and their interpretation are then dependent upon the tag value. The valid constant types and their tag values are:
Line 365 ⟶ 374:
==See also==
{{Portal|Computer programming}}
* [[Java bytecode]]
==References==
Line 380 ⟶ 389:
| url = https://docs.oracle.com/javase/specs/jvms/se6/html/ClassFile.doc.html
| access-date = 2008-10-13
}} The official defining document of the [[Java virtual machine|Java Virtual Machine]], which includes the class file format. Both the first and second editions of the book are freely available [https://docs.oracle.com/javase/specs/ online for viewing and/or download].
{{Java (Sun)}}
|