-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlow of Java Program One page
More file actions
182 lines (133 loc) · 3.67 KB
/
Copy pathFlow of Java Program One page
File metadata and controls
182 lines (133 loc) · 3.67 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
# Flow of a Java Program — One Page
The **flow of a Java program** explains what happens from the moment we write Java source code until the program produces output.
## 1. Write Java Source Code
We first create a Java source file with the `.java` extension.
```java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
```
File:
```text
Hello.java
```
---
## 2. Compilation
The Java compiler **`javac`** compiles the source code.
```text
Hello.java
↓
javac
↓
Hello.class
```
The compiler checks things such as:
* Syntax
* Data types
* Method declarations
* Class structure
If errors are found, compilation stops and we must correct the code.
---
## 3. Bytecode Generation
If compilation succeeds, the compiler generates a `.class` file containing **bytecode**.
```text
Java Source Code
↓
Compiler
↓
Bytecode
```
Bytecode is designed to be understood by the **JVM** rather than directly by a particular CPU.
---
## 4. Class Loading
When we run:
```text
java Hello
```
the JVM starts and loads the required classes into memory using its **class-loading mechanism**.
```text
.class files
↓
Class Loader
↓
JVM Memory
```
---
## 5. Bytecode Verification
The JVM verifies the bytecode before execution.
It performs checks to help ensure that the bytecode follows JVM rules and is structurally valid.
```text
Bytecode
↓
Verification
↓
Valid?
```
If verification fails, execution does not proceed normally.
---
## 6. Execution
The JVM executes the bytecode.
Modern JVMs can use both interpretation and **Just-In-Time (JIT) compilation**.
```text
Bytecode
↓
JVM
↓
Interpreter / JIT
↓
Native machine instructions
↓
CPU
```
The JIT compiler can optimize frequently executed code during runtime.
---
## 7. Output
Finally, the program produces the required result.
For our example:
```text
Hello, Java!
```
---
# Complete Flow
```text
┌──────────────────────┐
│ Java Source Code │
│ Hello.java │
└──────────┬───────────┘
↓
┌──────────────────────┐
│ Java Compiler │
│ javac │
└──────────┬───────────┘
↓
┌──────────────────────┐
│ Bytecode │
│ Hello.class │
└──────────┬───────────┘
↓
┌──────────────────────┐
│ Class Loader │
└──────────┬───────────┘
↓
┌──────────────────────┐
│ Bytecode Verifier │
└──────────┬───────────┘
↓
┌──────────────────────┐
│ JVM │
│ Interpreter + JIT │
└──────────┬───────────┘
↓
┌──────────────────────┐
│ CPU/OS │
└──────────┬───────────┘
↓
OUTPUT
```
### ⭐ Remember
> **Write → Compile → Bytecode → Load → Verify → Execute → Output**
Or simply:
**`.java → javac → .class → JVM → Output`**
This flow is the foundation for understanding how every Java program moves from **source code to execution**.