-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlow of Java Program IQs
More file actions
733 lines (518 loc) · 11.9 KB
/
Copy pathFlow of Java Program IQs
File metadata and controls
733 lines (518 loc) · 11.9 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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
# Flow of Java Program — DOUBT KILLER 🔥
The biggest confusion in Java is usually around **`.java`, `.class`, compiler, JVM, JDK, JRE, bytecode, interpreter, and JIT**. Let's connect everything.
---
## 1. First, memorize the basic flow
```text
.java
↓
javac
↓
.class
↓
JVM
↓
Execution
↓
Output
```
Example:
```text
Hello.java
↓
javac Hello.java
↓
Hello.class
↓
java Hello
↓
JVM
↓
Hello Java
```
Now let's kill the doubts one by one.
---
# 2. Is `.java` source code?
✅ **Yes.**
Example:
```java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello");
}
}
```
Stored as:
```text
Hello.java
```
This is **human-readable Java source code**.
---
# 3. What does `javac` do?
`javac` is the **Java compiler**.
It converts:
```text
.java
↓
.class
```
More precisely:
> It compiles Java source code into **JVM bytecode**.
Example:
```text
javac Hello.java
```
produces:
```text
Hello.class
```
---
# 4. Is `.class` machine code?
❌ **No.**
`.class` contains **JVM bytecode**.
Think:
```text
.java
↓
Java source code
↓
javac
↓
.class
↓
JVM bytecode
```
It is not ordinary CPU-specific machine code.
---
# 5. Then who converts bytecode into machine instructions?
The **JVM** handles execution.
Modern JVMs can use:
* interpretation
* JIT compilation
* runtime optimization
Conceptually:
```text
Bytecode
↓
JVM
↓
Interpreter / JIT
↓
Native machine instructions
↓
CPU
```
---
# 6. Is Java compiled or interpreted?
🔥 **Both concepts are involved.**
Java source is **compiled**:
```text
.java → bytecode
```
Then the JVM executes that bytecode using runtime execution mechanisms, including interpretation and JIT compilation.
So don't answer:
❌ "Java is only interpreted."
And don't answer:
❌ "Java is compiled directly into machine code."
Best answer:
> **Java source code is compiled into bytecode, and the JVM executes that bytecode, using JIT compilation to optimize frequently executed code.**
---
# 7. What exactly is JVM?
**JVM = Java Virtual Machine**
It is the runtime environment that executes Java bytecode.
It handles things such as:
* class loading
* bytecode verification
* execution
* memory management
* garbage collection
* threads
* runtime optimization
Think:
```text
Java Bytecode
↓
JVM
↓
Operating System
↓
Hardware
```
---
# 8. Is JVM platform-independent?
❌ **This is a classic trap.**
The **bytecode is designed to be portable**.
The JVM implementation itself is platform-specific.
```text
Same Bytecode
│
┌───────┼────────┐
↓ ↓ ↓
Windows JVM Linux JVM macOS JVM
↓ ↓ ↓
Windows Linux macOS
```
Therefore:
> **Java bytecode is portable; JVM implementations are platform-specific.**
---
# 9. Why does Java say "Write Once, Run Anywhere"?
Because you can compile your Java program to bytecode and run that bytecode on compatible JVMs for different platforms.
```text
Java Program
↓
Bytecode
↓
┌──────────┼──────────┐
↓ ↓ ↓
Windows JVM Linux JVM macOS JVM
```
That's the basic idea behind:
> **WORA — Write Once, Run Anywhere**
---
# 10. What is the difference between `javac` and `java`?
Very important!
### `javac`
Compiles.
```text
javac Hello.java
↓
Hello.class
```
### `java`
Launches the application.
```text
java Hello
↓
JVM starts
↓
Hello executes
```
### Memory trick
> **`javac` = compile**
> **`java` = run**
---
# 11. Why do we use `java Hello` instead of `java Hello.class`?
Because the Java launcher expects the **class name** in the normal invocation.
```text
java Hello
```
not:
```text
java Hello.class
```
The JVM/runtime locates the appropriate class using the class path/module path and related mechanisms.
---
# 12. What happens after `java Hello`?
This is where the deeper flow starts:
```text
java Hello
↓
JVM starts
↓
Class Loader
↓
Load Hello
↓
Verification / Linking
↓
Initialization
↓
main()
↓
Bytecode execution
↓
Output
```
---
# 13. What does Class Loader do?
The JVM needs class definitions before it can execute them.
The **Class Loader** loads them into the JVM.
```text
Hello.class
↓
Class Loader
↓
JVM
```
It can also load classes required by your program.
For example:
```java
Student s = new Student();
```
The JVM may need to load the `Student` class definition.
---
# 14. Is loading the same as initialization?
❌ **No.**
This is an important advanced distinction.
```text
Loading
↓
Linking
↓
Initialization
```
A class can be loaded before it is initialized.
---
# 15. What happens during linking?
Linking is commonly described as:
```text
Linking
├── Verification
├── Preparation
└── Resolution
```
### Verification
Checks bytecode validity.
### Preparation
Sets up memory for class-level data and establishes appropriate default values.
### Resolution
Resolves symbolic references as needed.
---
# 16. What is class initialization?
Suppose:
```java
class Test {
static int x = 100;
static {
System.out.println("Hello");
}
}
```
When the class is initialized, its static initialization actions are performed.
So:
```text
Load
↓
Link
↓
Initialize
↓
Use class
```
---
# 17. Where does `main()` fit?
For a traditional standalone Java application:
```java
public static void main(String[] args)
```
is the conventional entry point.
So:
```text
JVM
↓
Load class
↓
Initialize as required
↓
main()
↓
Application code
```
But remember:
> Not every Java class needs a `main()` method.
A class can simply be a library class used by another application.
---
# 18. What is JIT?
**JIT = Just-In-Time compiler.**
Suppose:
```java
for (int i = 0; i < 1_000_000; i++) {
calculate(i);
}
```
`calculate()` may become very frequently executed.
The JVM can detect this **hot code** and compile/optimize it at runtime.
```text
Bytecode
↓
JVM observes execution
↓
Hot code
↓
JIT
↓
Optimized native code
```
So the JVM is not simply interpreting everything forever.
---
# 19. Does JIT compile the whole program before execution?
❌ Not necessarily.
Modern JVM execution is adaptive.
Different code can be handled differently depending on runtime behavior.
Conceptually:
```text
Cold code → may be interpreted
Hot code → JIT optimized
```
The exact strategy depends on the JVM implementation and runtime conditions.
---
# 20. Where does Garbage Collection fit?
Garbage collection happens **during program execution**.
Suppose:
```java
Student s = new Student();
```
Later the object becomes unreachable.
```text
Object created
↓
Object used
↓
No longer reachable
↓
Eligible for GC
↓
Garbage Collector
↓
Memory may be reclaimed
```
GC is a JVM runtime activity; it is **not a compilation step**.
---
# 21. Does GC immediately delete an object?
❌ No.
An unreachable object becomes **eligible** for garbage collection.
The JVM decides when and how memory should be reclaimed.
So:
> **Unreachable ≠ immediately destroyed**
---
# 22. Stack vs Heap — where do they fit?
During execution, the JVM manages runtime memory.
A simplified model:
```text
JVM
├── Stack
│ └── Method frames
│
└── Heap
└── Objects
```
Example:
```java
Student s = new Student();
```
Conceptually:
```text
Stack Heap
───── ────
s ──────────────────→ Student object
```
This is a useful learning model, although real JVM/JIT implementations can optimize storage in ways that don't always match this simplified picture exactly.
---
# 23. Is JDK part of program execution?
The **JDK is primarily the development kit**.
You use tools such as:
```text
javac
java
jar
javadoc
```
The important distinction is:
```text
JDK → Development
JVM → Runtime execution
```
Modern JDK distributions include the runtime components needed to run Java applications.
---
# 24. What about JRE?
You may see the traditional diagram:
```text
JDK
↓
JRE
↓
JVM
```
Historically, this was used to explain:
* JDK → development
* JRE → runtime environment
* JVM → bytecode execution
But modern Java distributions generally don't require a separately installed standalone JRE.
For learning the execution flow, focus on:
> **JDK tools compile and launch; the JVM executes.**
---
# 25. The Complete Doubt-Killer Diagram 🔥
```text
WRITE
│
▼
Hello.java
│
▼
javac compiler
│
COMPILE
│
▼
Hello.class
│
BYTECODE
│
▼
java Hello
│
▼
JVM
│
▼
Class Loader
│
▼
Verification
│
▼
Linking
┌────────┼────────┐
│ │ │
Verify Prepare Resolve
└────────┼────────┘
▼
Initialization
│
▼
main()
│
▼
Bytecode Execution
│
┌─────┴─────┐
▼ ▼
Interpreter JIT
│ │
└─────┬─────┘
▼
Native Execution
│
▼
CPU
│
▼
OUTPUT
Meanwhile, JVM manages:
• Heap
• Stacks
• Garbage Collection
• Threads
```
---
# 🚨 10 Most Common Exam Traps
| Question | Correct answer |
| ----------------------------- | ------------------------------------------------- |
| `.java` contains? | **Source code** |
| `.class` contains? | **JVM bytecode** |
| Who compiles Java source? | **`javac`** |
| Who executes bytecode? | **JVM** |
| `javac` or `java` to compile? | **`javac`** |
| `javac` or `java` to run? | **`java`** |
| Is bytecode machine code? | **No** |
| Is JVM platform-independent? | **No, JVM implementations are platform-specific** |
| Why is Java portable? | **Bytecode + compatible JVMs** |
| Does Java use JIT? | **Modern JVMs do** |
---
# 🧠 Ultimate Memory Trick
Remember this sentence:
> **Write → Compile → Bytecode → Load → Link → Initialize → Execute → Optimize → Output**
Or the exam-friendly short form:
> **`.java → javac → .class → JVM → Output`**
And the **deep technical form**:
> **Source → Bytecode → Class Loading → Linking → Initialization → Runtime Execution → JIT Optimization → Native Instructions → CPU**
If you understand those three versions, you have the **complete flow of a Java program from beginner to interview level**.