-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlow of Java Program 3 Level
More file actions
220 lines (170 loc) · 4 KB
/
Copy pathFlow of Java Program 3 Level
File metadata and controls
220 lines (170 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# Flow of Java Program — 3 Levels
## 🟢 Level 1 — Beginner
Think of a Java program as a simple journey:
```text
Java Source Code
↓
Compile
↓
Bytecode
↓
JVM
↓
Output
```
### Example
You write:
```java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
```
Save it as:
```text
Hello.java
```
Compile:
```text
javac Hello.java
```
This produces:
```text
Hello.class
```
Then run:
```text
java Hello
```
The JVM executes the bytecode and produces:
```text
Hello Java
```
### ⭐ Remember
> **`.java → javac → .class → JVM → Output`**
---
# 🟡 Level 2 — Intermediate
Now add the important internal steps.
```text
Hello.java
↓
Java Compiler
javac
↓
Bytecode
Hello.class
↓
Class Loader
↓
Verification
↓
Linking
↓
Initialization
↓
JVM Execution
↓
Interpreter / JIT
↓
CPU / OS
↓
Output
```
### What happens at each stage?
**1. Source Code**
You write Java instructions in `.java`.
**2. Compilation**
`javac` checks the code and converts it into bytecode.
**3. Bytecode**
Stored in `.class` files and designed for the JVM.
**4. Class Loading**
The JVM loads required classes.
**5. Verification**
The JVM checks that the bytecode is valid.
**6. Linking**
The JVM prepares the class for execution.
**7. Initialization**
Static initialization is performed when required.
**8. Execution**
The JVM executes the program.
---
# 🔴 Level 3 — Advanced
At the JVM level, the flow becomes:
```text
Java Source
│
▼
javac
│
▼
JVM Bytecode
│
▼
Class Loading
│
├── Loading
│
└── Linking
├── Verification
├── Preparation
└── Resolution
│
▼
Class Initialization
│
▼
JVM Runtime
│
├── Heap
├── Java Stacks
├── PC Registers
└── Other runtime areas
│
▼
Bytecode Execution
│
├── Interpreter
│
└── JIT Compiler
↓
Native Machine Code
↓
CPU
↓
Output
```
### Where does memory fit?
During execution, the JVM manages runtime memory.
```text
JVM
├── Heap
│ └── Objects
│
├── Java Stack
│ └── Method frames
│
└── Other runtime areas
```
For example:
```java
Student s = new Student();
```
Conceptually:
```text
Stack Heap
───── ────
s ──────────────────→ Student object
```
The **garbage collector** can later reclaim heap memory occupied by objects that are no longer reachable.
---
# 🔥 The 3-Level Revision
| Level | Flow |
| --------------- | -------------------------------------------------------------------------------------------------------------------- |
| 🟢 Beginner | `.java → javac → .class → JVM → Output` |
| 🟡 Intermediate | `Source → Compile → Bytecode → Load → Verify → Link → Initialize → Execute` |
| 🔴 Advanced | `Source → Bytecode → Class Loading → Linking → Initialization → Runtime Areas → Interpreter/JIT → Native Code → CPU` |
### 🧠 Ultimate memory trick
**W → C → B → L → V → I → E → O**
**Write → Compile → Bytecode → Load → Verify/Link → Initialize → Execute → Output**
> **Java source code becomes bytecode; the JVM loads, verifies, links, initializes, and executes that bytecode, using interpretation and JIT compilation as appropriate.**