Java class file: Difference between revisions

Content deleted Content added
Citation bot (talk | contribs)
Add: title. Changed bare reference to CS1/2. | Use this bot. Report bugs. | Suggested by BrownHairedGirl | Linked from User:BrownHairedGirl/Articles_with_bare_links | #UCB_webform_linked 656/2843
 
(23 intermediate revisions by 21 users not shown)
Line 1:
{{Short description|Executable Java file format}}
{{About|the data format|classes in Java|Class (computer programming)}}
{{Infobox file format
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 virtual machine|Java Virtual Machine (JVM)]]. A Java class file is usually produced by a [[Java compiler]] from [[Java (programming language)|Java programming language]] [[source file]]s ({{mono|.java}} files) containing Java [[Class (programming)|classes]] (alternatively, other [[JVM languages]] can also be used to create class files). If a source file has more than one class, each class is compiled into a separate class file. Thus, it is called a {{mono|.class}} file because it contains the bytecode for a single class.
 
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 (computingcomputer programming)|Method]]s''': Any methods in the class
* '''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"
|-
! byteByte offset
! sizeSize
! typeType or value
! Description
! 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/se17se23/html/jvms-4.html#jvms-4.1-200-B.2|title = ChapterTable 4. The1-A. class Filefile format major Formatversions}}</ref><br />
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 inof a C-likeclass programming languagefile===
The following is a representation of a {{mono|.class}} file as if it were a C-style struct.
Since [[C (programming language)|C]] doesn't support multiple variable length arrays within a struct, the code below won't compile and only serves as a demonstration.
<syntaxhighlight lang="ccpp">
struct Class_File_FormatClassFileFormat {
u4 magic_numbermagicNumber;
 
u2 minor_versionminorVersion;
u2 major_versionmajorVersion;
 
u2 constant_pool_countconstantPoolCount;
ConstantPoolInfo[constantPoolCount - 1] constantPool;
cp_info constant_pool[constant_pool_count - 1];
 
u2 access_flagsaccessFlags;
 
u2 this_classthisClass;
u2 super_classsuperClass;
 
u2 interfaces_countinterfacesCount;
u2[interfacesCount] interfaces;
u2 interfaces[interfaces_count];
 
u2 fields_countfieldsCount;
FieldInfo[fieldsCount] fields;
field_info fields[fields_count];
 
u2 methods_countmethodsCount;
MethodInfo[methodsCount] methods;
method_info methods[methods_count];
 
u2 attributes_countattributesCount;
AttributeInfo[attributesCount] attributes;
attribute_info attributes[attributes_count];
}
</syntaxhighlight>