-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava_Introduction_IQs
More file actions
553 lines (375 loc) · 9.76 KB
/
Copy pathJava_Introduction_IQs
File metadata and controls
553 lines (375 loc) · 9.76 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
# Java Introduction — Doubt Killer 🔥
Let's eliminate the most common confusion around **Java, JDK, JRE, JVM, bytecode, compiler, and platform independence**.
---
## 1. Is Java a language or a platform?
**Both, depending on what you mean.**
### Java language
The programming language used to write code:
```java
int age = 20;
```
### Java platform
The larger environment containing the JVM, standard APIs, tools, and ecosystem.
So in exams:
> **Java is a high-level, object-oriented programming language and a platform.**
---
# 2. Java vs JVM — same thing?
❌ **No.**
Think:
```text
Java = Language
JVM = Runtime that executes Java bytecode
```
Example:
```java
System.out.println("Hello");
```
is **Java code**.
The JVM is what ultimately executes the compiled bytecode produced from that code.
---
# 3. What is JDK?
**JDK = Java Development Kit**
It is used when you want to **develop Java programs**.
It provides tools such as:
```text
javac → compiler
java → launcher
javadoc → documentation tool
jar → archive tool
```
Simplified:
```text
JDK
├── Development tools
├── Java libraries
└── JVM/runtime components
```
### Remember:
> **JDK = Develop Java**
---
# 4. What is JRE?
**JRE = Java Runtime Environment**
Historically, this term referred to the environment required to **run** Java applications.
The classic teaching model is:
```text
JDK
↓
JRE
↓
JVM
```
But there's an important modern clarification:
> Modern JDK distributions generally provide the runtime directly, and a separate standalone JRE is no longer the normal distribution model.
For exams, however, you may still encounter the traditional:
**JDK > JRE > JVM** model.
---
# 5. JDK vs JRE vs JVM
| Component | Main purpose |
| --------- | ------------------------------- |
| **JDK** | Develop Java programs |
| **JRE** | Historically, run Java programs |
| **JVM** | Execute Java bytecode |
### Memory trick 🧠
**JDK → Develop**
**JRE → Run**
**JVM → Execute**
---
# 6. What is bytecode?
When you write:
```text id="4s0xye"
Hello.java
```
the Java compiler converts it into:
```text id="x5k6h8"
Hello.class
```
The `.class` file contains **bytecode**.
So:
```text id="j8qf8q"
Java Source Code
↓
javac
↓
Bytecode
↓
JVM
```
Bytecode is an **intermediate instruction format for the JVM**.
---
# 7. Is bytecode machine code?
❌ Not in the usual sense.
Don't confuse:
```text
Source code
Bytecode
Native machine code
```
They are different layers.
```text id="9p0v6w"
Java Source
↓
Bytecode
↓
JVM/JIT
↓
Native machine instructions
```
---
# 8. Why is Java platform-independent?
This is probably the **most important Java question**.
Java source code is compiled into bytecode.
The bytecode can run on different platforms through their respective JVM implementations.
```text id="f4z3kt"
Bytecode
↓
┌────────┼────────┐
↓ ↓ ↓
JVM JVM JVM
Windows Linux macOS
```
Therefore:
> **Java is platform-independent at the bytecode/application level, while the JVM itself is platform-specific.**
🔥 That's the deeper answer.
---
# 9. Is the JVM platform-independent?
❌ No.
This is a common trick question.
The **Java program/bytecode** is designed to be portable.
But the JVM implementation is specific to its platform.
```text id="w0f2zv"
Java Bytecode
↓
┌────┼─────┐
↓ ↓ ↓
JVM JVM JVM
Win Linux macOS
```
So:
> **Java bytecode is portable; JVM implementations are platform-specific.**
---
# 10. Is Java compiled or interpreted?
The correct answer is:
> **Java uses both compilation and runtime execution techniques.**
First:
```text
Java source
↓
javac
↓
Bytecode
```
Then the JVM can interpret bytecode and/or use **JIT compilation** to compile frequently executed code into native machine code.
So don't simply say:
❌ "Java is only interpreted."
And don't say:
❌ "Java is compiled directly into machine code."
Better:
> **Java source is compiled to bytecode, which is executed by the JVM, with modern JVMs using JIT compilation for performance.**
---
# 11. What is JIT?
**JIT = Just-In-Time compiler.**
Suppose the JVM notices that a method is executed millions of times.
It can optimize and compile that frequently executed code into native machine code.
```text id="m8byc6"
Bytecode
↓
JVM observes execution
↓
Hot code
↓
JIT compiler
↓
Optimized native code
```
This is one reason modern Java can be very fast.
---
# 12. Does Java have a compiler?
✅ Yes.
The traditional Java compiler is:
```text id="y8i7aq"
javac
```
It converts:
```text
.java
↓
.class
```
Example:
```text id="q7m4x8"
Hello.java
↓
javac Hello.java
↓
Hello.class
```
---
# 13. What is garbage collection?
Java normally doesn't require programmers to manually free ordinary objects.
Example:
```java
Student s = new Student();
```
When an object becomes unreachable, the JVM's **garbage collector** may eventually reclaim its memory.
```text id="1b3tne"
Object created
↓
Object used
↓
No references
↓
Garbage Collector
↓
Memory reclaimed
```
### Important:
Garbage collection does **not** mean Java can never have memory problems.
If your program accidentally keeps references to unnecessary objects, those objects may remain reachable and consume memory.
---
# 14. Is Java 100% object-oriented?
This depends on how the question is being asked.
Java is fundamentally **object-oriented**, but it has **primitive data types** such as:
```java
int
char
boolean
double
```
So a precise answer is:
> Java is a strongly object-oriented, class-based language, but it includes primitive types for efficiency and convenience.
---
# 15. Why does Java have primitive types?
Instead of:
```java
Integer x = 10;
```
you can use:
```java
int x = 10;
```
Primitives provide efficient representations for basic values.
Java also provides wrapper types:
```text
int → Integer
long → Long
double → Double
boolean → Boolean
```
This allows Java to combine primitive efficiency with object-based APIs.
---
# 16. Is Java the same as JavaScript?
🚨 **Absolutely not.**
They are separate programming languages.
| Java | JavaScript |
| ---------------------------- | ---------------------------- |
| JVM ecosystem | Browser/Node.js ecosystems |
| Class-based | Prototype-based historically |
| Statically typed | Dynamically typed |
| `.java` | `.js` |
| Common in enterprise/backend | Common in web development |
Their similar names are historical, not evidence that they're the same language.
---
# 17. What does `public static void main` mean?
Example:
```java
public static void main(String[] args) {
System.out.println("Hello");
}
```
For a traditional standalone Java application:
* `public` → accessible to the runtime
* `static` → associated with the class
* `void` → returns nothing
* `main` → conventional entry-point name
* `String[] args` → command-line arguments
---
# 18. Is `main()` the only way Java programs run?
❌ No.
`main()` is the traditional entry point for standalone Java applications.
Java applications can also run through:
* application servers
* frameworks
* test runners
* build tools
* containers
* other launch mechanisms
So don't think:
> "Every piece of Java code must have a main method."
A Java class can simply be part of a larger application.
---
# 19. Is Java only used for desktop applications?
❌ Definitely not.
Java is heavily used for:
```text
Enterprise applications
Backend services
REST APIs
Cloud applications
Banking systems
Distributed systems
Data-processing systems
Developer tools
```
Historically, Java was also famous for browser applets and played a major role in early Android development.
---
# 20. Why is Java still popular?
Because it combines:
```text
Portability
+
Strong typing
+
Garbage collection
+
Powerful JVM
+
Huge ecosystem
+
Backward compatibility
+
Enterprise adoption
```
That combination made Java extremely durable.
---
# 🚨 Final Doubt Killer
Memorize this diagram:
```text
JAVA
│
Source Code
.java
│
↓
javac compiler
│
↓
Bytecode
.class
│
↓
JVM
┌────────┼────────┐
↓ ↓ ↓
GC JIT Runtime
│ │ │
└────────┼────────┘
↓
Native execution
↓
Operating System
↓
Hardware
```
### 🔥 10-second revision
**Java** → language/platform
**JDK** → development kit
**JRE** → traditional runtime concept
**JVM** → executes bytecode
**`javac`** → compiler
**`.java`** → source file
**`.class`** → bytecode file
**Bytecode** → JVM instructions
**JIT** → runtime compilation/optimization
**GC** → automatic memory reclamation
### ⭐ The ultimate answer
> **Java source code is compiled by `javac` into platform-neutral bytecode. That bytecode runs on a platform-specific JVM, which provides runtime services such as memory management and can use JIT compilation to optimize execution. This architecture is the foundation of Java's platform independence.**