Skip to content

Commit 6089595

Browse files
committed
Documentation updated
1 parent bbc4dc0 commit 6089595

File tree

4 files changed

+81
-18
lines changed

4 files changed

+81
-18
lines changed

doc/About JavOC/Serialization/Constant Pool.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Constant pool entries serialization
22

3-
## Structrue of serialized entry
3+
## Structure of serialized entry
44
For debug purposes, it is very convenient to have a way to represent a constant pool entry in a *human-readable form*.
55

66
However, since most of entries contain links to other entries, that link to other constants as well... It becomes tricker to handle and represent.

doc/Class Loading and structure/Class File Format.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Each `.class` value starts with special 'magic value' - a sequence of 4 bytes, t
4444

4545
### Minor and major versions
4646
<img align="right" src="../Diagrams/Class%20Loading%20and%20structure/Versions.png" alt="drawing" width="200"/>
47-
Each class was compiled to be used with a specific version of Java. It was quite complex back in old versions of Java, but today everything is pretty straightforward - JVM can run classes of its version and classes with *older* versions. In other words, JVMs are **backwards compatible**
47+
Each class was compiled to be used with a specific version of Java. It was quite complex back in old versions of Java, but today everything is pretty straightforward - JVM can run classes of its version and classes with <em>older</em> versions. In other words, JVMs are <b>backwards compatible</b>
4848

4949
To store version of the Java that compiled the file, Major and Minor fields exist
5050

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Java class file structure by example
2-
As you already know from "Class File Format" document, Java classes have a relatively complex structure. And the best way to study it - compile something and see these beatiful files in the wild!
2+
As you already know from [Class File Format](Class%20File%20Format.md) document, Java classes have a relatively complex structure. And the best way to study it - compile something and see these beatiful files in the wild!
33

44
For such a purpose, you can use `javac` and `javad` command line tools - official compiler and disassembler, that are included into every Java Development Kit.
55

@@ -11,21 +11,21 @@ For the beginning, let's compile a simple class.
1111

1212
Something like **this**:
1313
```java
14-
class Main {}
14+
class SimplestClass {}
1515
```
1616
<details>
1717
<summary>Disassembler output</summary>
1818

1919

20-
Classfile /tmp/1059625032030680147/classes/Main.class
20+
Classfile /tmp/1059625032030680147/classes/SimplestClass.class
2121
Last modified Aug 7, 2020; size 237 bytes
2222
SHA-256 checksum <>
23-
Compiled from "Main.java"
24-
class Main
23+
Compiled from "SimplestClass.java"
24+
class SimplestClass
2525
minor version: 0
2626
major version: 58
2727
flags: (0x0020) ACC_SUPER
28-
this_class: #7 // Main
28+
this_class: #7 // SimplestClass
2929
super_class: #2 // java/lang/Object
3030
interfaces: 0, fields: 0, methods: 1, attributes: 1
3131

@@ -36,38 +36,81 @@ class Main {}
3636
#4 = Utf8 java/lang/Object
3737
#5 = Utf8 <init>
3838
#6 = Utf8 ()V
39-
#7 = Class #8 // Main
40-
#8 = Utf8 Main
39+
#7 = Class #8 // SimplestClass
40+
#8 = Utf8 SimplestClass
4141
#9 = Utf8 Code
4242
#10 = Utf8 LineNumberTable
4343
#11 = Utf8 LocalVariableTable
4444
#12 = Utf8 this
45-
#13 = Utf8 LMain;
45+
#13 = Utf8 LSimplestClass;
4646
#14 = Utf8 SourceFile
47-
#15 = Utf8 Main.java
47+
#15 = Utf8 SimplestClass.java
4848

4949
{
50-
Main();
50+
SimplestClass();
5151
descriptor: ()V
5252
flags: (0x0000)
5353

5454
Code:
5555

5656
stack=1, locals=1, args_size=1
57-
start local 0 s// Main this
57+
start local 0 s// SimplestClass this
5858
0: aload_0
5959
1: invokespecial #1 // Method java/lang/Object."<init>":()V
6060
4: return
61-
end local 0 // Main this
61+
end local 0 // SimplestClass this
6262

6363
LineNumberTable:
6464

6565
LocalVariableTable:
6666

6767
Start Length Slot Name Signature
68-
0 5 0 this LMain;
68+
0 5 0 this LSimplestClass;
6969

7070
}
71-
SourceFile: "Main.java"
71+
SourceFile: "SimplestClass.java"
72+
</details>
7273

73-
</details>
74+
Okay, hold on. We have compiled the simplest class we could have, right? Where all of this code came from?
75+
76+
Well, if you are familiar with Java, you surely know, that Java compilers actually add something to your code. Things like default constructor, implicit inheritance from `java.lang.Object`...
77+
78+
Long story short, that's how our class *actually* looks after compilation:
79+
80+
```java
81+
class SimplestClass extends java.lang.Object {
82+
public void <init> { // That's our default constructor
83+
java.lang.Object.<init>(); // Calling a constructor from Object
84+
return;
85+
}
86+
}
87+
```
88+
89+
Obviously, this code won't compile! But that's how our class really looks under the hood.
90+
91+
That explains everything. But let's dive into each line of decomiler's output and see what it really does!
92+
## `javap` output teardown
93+
94+
`javap` output starts with some information about class file - creation date, size, etc. :
95+
```java
96+
Classfile /tmp/1059625032030680147/classes/SimplestClass.class
97+
Last modified Aug 7, 2020; size 237 bytes
98+
SHA-256 checksum <>
99+
Compiled from "SimplestClass.java"
100+
```
101+
102+
After that, the class file name goes:
103+
```java
104+
class SimplestClass
105+
```
106+
107+
And the major and minor versions of the JDK that compiled the file. Looking up the version of the Java in the special [table](/doc/Reference/Java%20major%20versions.md), we gain a resulting version of Java - **15**!
108+
```java
109+
minor version: 0
110+
major version: 59
111+
```
112+
113+
Class flags:
114+
```java
115+
flags: (0x0020) ACC_SUPER
116+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Java major versions
2+
| Version | Decimal | Hex |
3+
| :------------ | :------ | :------- |
4+
| JDK 1.1 | 45 | 0x2D |
5+
| JDK 1.2 | 46 | 0x2E |
6+
| JDK 1.3 | 47 | 0x2F |
7+
| JDK 1.4 | 48 | 0x30 |
8+
| Java SE 5 | 49 | 0x31 |
9+
| **Java SE 6** | **50** | **0x32** |
10+
| Java SE 7 | 51 | 0x33 |
11+
| Java SE 8 | 52 | 0x34 |
12+
| Java SE 9 | 53 | 0x35 |
13+
| Java SE 10 | 54 | 0x36 |
14+
| Java SE 11 | 55 | 0x37 |
15+
| Java SE 12 | 56 | 0x38 |
16+
| Java SE 13 | 57 | 0x39 |
17+
| Java SE 14 | 58 | 0x3A |
18+
| Java SE 15 | 59 | 0x3B |
19+
| Java SE 16 | 60 | 0x3C |
20+
| Java SE 17 | 61 | 0x3D |

0 commit comments

Comments
 (0)