-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlow of Java Program Teaching Style
More file actions
579 lines (388 loc) · 7.91 KB
/
Copy pathFlow of Java Program Teaching Style
File metadata and controls
579 lines (388 loc) · 7.91 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# Flow of a Java Program — Teach Me 🎓
Let's learn the **flow of a Java program** from absolute zero. The goal is to understand **what happens to your code after you write it**.
---
# 1. The Big Picture
Whenever you write a Java program, think of this journey:
```text
Write Code
↓
Compile
↓
Bytecode
↓
Load
↓
Verify
↓
Execute
↓
Output
```
The most important version to memorize is:
> **`.java → javac → .class → JVM → Output`**
Now let's understand what each step actually means.
---
# 2. Step 1 — Write Java Code ✍️
You create a file ending in `.java`.
Example:
```java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
```
Save it as:
```text
Hello.java
```
This is called **source code**.
At this stage, the computer hasn't executed your program yet.
---
# 3. Step 2 — Compile the Program
We use the Java compiler:
```text
javac
```
Command:
```text
javac Hello.java
```
The compiler checks your code.
For example, this is wrong:
```java
int age = "Hello";
```
because `age` is an `int`, while `"Hello"` is a `String`.
If there are compilation errors:
```text
Source Code
↓
Compiler
↓
ERROR ❌
```
The program doesn't produce usable bytecode until the errors are fixed.
---
# 4. Step 3 — Bytecode Is Created
If compilation succeeds:
```text
Hello.java
↓
javac
↓
Hello.class
```
The `.class` file contains **bytecode**.
Think of bytecode as a middle language.
```text
Java language
↓
Bytecode
↓
JVM
```
This is one of the most important ideas in Java.
---
# 5. Why not directly create machine code?
Because Java wanted programs to be portable.
Imagine:
```text
Java Program
↓
Bytecode
↓
┌───┼────┐
↓ ↓ ↓
Win Linux Mac
JVM JVM JVM
```
The same bytecode can be executed by JVM implementations for different platforms.
That's the basic idea behind:
> **Write Once, Run Anywhere**
---
# 6. Step 4 — Run the Program
Now we execute:
```text
java Hello
```
Notice something interesting:
When compiling, we write:
```text
javac Hello.java
```
When running, we write:
```text
java Hello
```
We don't normally write:
```text
java Hello.class
```
The Java launcher starts the JVM and requests execution of the `Hello` class.
---
# 7. Step 5 — Class Loading
The JVM needs the class before it can execute it.
So the **Class Loader** loads the class.
```text
Hello.class
↓
Class Loader
↓
JVM
```
Classes can be loaded when they are needed.
For example, if your application uses another class:
```java
Student s = new Student();
```
the JVM may need to load `Student` as well.
---
# 8. Step 6 — Verification
The JVM checks the bytecode before execution.
Think:
> "Is this bytecode valid according to JVM rules?"
```text
Bytecode
↓
Verifier
↓
Valid?
```
This is an important part of the JVM's runtime safety architecture.
---
# 9. Step 7 — Class Initialization
Before certain classes are actively used, the JVM performs class initialization.
For example:
```java
class Test {
static int x = 100;
static {
System.out.println("Initialized");
}
}
```
The JVM performs the necessary initialization when the class is initialized.
Don't confuse:
```text
Loading ≠ Initialization
```
A class can be loaded before it is initialized.
---
# 10. Step 8 — `main()` Starts
For a traditional standalone Java application, execution begins with:
```java
public static void main(String[] args)
```
Example:
```java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
```
The JVM/runtime invokes `main()`.
Then your application starts doing its actual work.
---
# 11. Step 9 — JVM Executes the Bytecode
Now comes the interesting part.
The JVM can execute bytecode using interpretation and can also use **JIT compilation**.
### Interpretation
Conceptually:
```text
Bytecode
↓
Interpreter
↓
Execute
```
### JIT compilation
Frequently executed code can be compiled into optimized native machine code:
```text
Bytecode
↓
JVM observes execution
↓
Frequently executed code
↓
JIT compiler
↓
Native machine code
```
This happens during runtime.
---
# 12. What is JIT?
**JIT = Just-In-Time compiler.**
Suppose this method is called thousands or millions of times:
```java
calculate();
calculate();
calculate();
calculate();
```
The JVM can recognize that this is **hot code**.
It may optimize it.
So:
```text
Rare code
↓
May remain interpreted
Hot code
↓
JIT compilation
↓
Optimized native code
```
This is a major reason modern Java can perform very well.
---
# 13. Step 10 — Memory Management
While your program runs, the JVM manages memory.
The JVM has several runtime memory areas.
The two you'll encounter first are:
### Stack
Used for method execution and stack frames.
### Heap
Generally where Java objects are allocated.
Example:
```java
Student s = new Student();
```
A simplified picture:
```text
Stack Heap
───── ────
s ───────────────────→ Student object
```
---
# 14. What happens when a method is called?
Suppose:
```java
main()
↓
calculate()
↓
add()
```
The stack conceptually looks like:
```text
┌─────────────┐
│ add() │ ← current
├─────────────┤
│ calculate() │
├─────────────┤
│ main() │
└─────────────┘
```
When `add()` finishes, its stack frame disappears.
Then `calculate()` continues.
---
# 15. Garbage Collection ♻️
Java automatically manages ordinary object memory using **garbage collection**.
Suppose:
```java
Student s = new Student();
```
Later:
```java
s = null;
```
If nothing else refers to the object:
```text
Student object
↓
No longer reachable
↓
Eligible for garbage collection
↓
Memory may eventually be reclaimed
```
Important:
> **Garbage collection doesn't necessarily happen immediately.**
The JVM decides when and how to perform collection.
---
# 16. Finally — Output
Our original program:
```java
System.out.println("Hello Java");
```
produces:
```text
Hello Java
```
So the entire journey was:
```text
Hello.java
↓
javac
↓
Hello.class
↓
Bytecode
↓
Class Loader
↓
Verification / Linking
↓
Initialization
↓
JVM Execution
↓
Interpreter / JIT
↓
CPU
↓
Hello Java
```
---
# 🧠 One Simple Story
Imagine Java is a restaurant.
### 👨🍳 You
Write the recipe:
```text
Hello.java
```
### 📝 Compiler
Converts the recipe into a standard instruction format:
```text
Bytecode
```
### 🚪 Class Loader
Brings the required instructions into the JVM.
### 🔍 Verifier
Checks that the instructions are valid.
### 🏭 JVM
Runs the instructions.
### ⚡ JIT
Optimizes frequently repeated work.
### ♻️ Garbage Collector
Cleans up objects that are no longer reachable.
### 🖥️ CPU
Actually performs the machine-level operations.
### 🍽️ Output
You get:
```text
Hello Java
```
---
# 🎯 Three Levels to Remember
### 🟢 Beginner
> **Write → Compile → Run**
```text
.java
↓
javac
↓
.class
↓
JVM
↓
Output
```
### 🟡 Intermediate
> **Source → Bytecode → Class Loading → Verification → Execution**
### 🔴 Advanced
> **Source → Compilation → Bytecode → Loading → Linking → Initialization → Runtime execution → JIT optimization → Native execution**
---
## 🔥 The one sentence you should remember
> **A Java program is written as source code, compiled by `javac` into JVM bytecode, loaded and prepared by the JVM, and then executed using JVM runtime mechanisms such as interpretation and JIT compilation to produce the final result.**