History and Features of Java
How Java Works (Compilation and Execution)
Java is both a compiled and interpreted language. Here's how a Java program runs from source code to
execution:
1. **Writing the Code (.java file)**
- A developer writes code in a `.java` file using a text editor or IDE.
- Example: `[Link]`
2. **Compilation (javac)**
- The Java Compiler (`javac`) compiles the `.java` file.
- It checks for syntax errors and converts the source code into **bytecode**.
- Output: A `.class` file (e.g., `[Link]`).
3. **Bytecode**
- Bytecode is an intermediate, platform-independent code.
- It is not directly executable by the OS.
- Ensures Java's 'Write Once, Run Anywhere' feature.
4. **Execution (java)**
- The Java Virtual Machine (`java`) loads the `.class` file.
- JVM interprets or compiles the bytecode into machine code using the **Just-In-Time (JIT) compiler**.
- The program is then executed by the CPU.
-> Example Flow:
History and Features of Java
1. Write code: `[Link]`
2. Compile: `javac [Link]` -> generates `[Link]`
3. Run: `java HelloWorld` -> JVM executes the bytecode
**Summary:**
- `.java` -> compiled by `javac` -> `.class` (bytecode)
- `.class` -> run by `java` command -> executed by JVM