Introduction
Today I learned Java basics from my trainer, and he explained it in a simple and interesting way. He started with a story about why Java was created and how it solved problems that existed in C++.
This explanation helped me understand not just the syntax, but the real purpose behind Java.
The Problem with C++
Before Java, C++ was widely used for building applications, systems, and even some websites. It is a powerful and fast language because it directly converts code into machine-level instructions.
But it had one major problem: platform dependency.
In C++:
- Code is directly compiled into binary (machine code)
- This binary is specific to a particular operating system
That means:
- A program written for Windows will not work on Linux or macOS
- Developers need to recompile or modify code for each system
This made development time-consuming and less flexible.
The Birth of Java
To solve this issue, Java was introduced with a new concept:
“Write Once, Run Anywhere”
Instead of directly converting code into machine code, Java introduced an intermediate step called bytecode.
This changed everything.
Levels of Code in Java
Java works with three levels of code:
- High-Level Code
- The code written by programmers
- Easy to read and understand
- Example: Java syntax
- Bytecode (Mid-Level)
- Intermediate code generated after compilation
- Platform-independent
- Machine Code (Low-Level / Binary)
- Executed by the computer
- Consists of 0s and 1s
Role of JDK (Java Development Kit)
The JDK is used to develop Java programs.
It includes:
- Compiler (
javac) - Tools required for development
Main function:
- Converts high-level Java code → bytecode
So the process starts like this:
Java Code → JDK Compiler → Bytecode
Understanding JVM and JRE
Java does not stop at bytecode. It needs to run the program, and that is where JVM and JRE come in.
JVM (Java Virtual Machine)
- Responsible for executing bytecode
- Converts bytecode into machine code
- Makes Java platform-independent
JRE (Java Runtime Environment)
- Provides environment to run Java programs
-
Contains:
- JVM
- Required libraries
Relationship
JDK = JRE + Development Tools
JRE = JVM + Libraries
Role of JIT Compiler
Inside the JVM, there is an important component called the JIT (Just-In-Time) Compiler.
Its function:
- Converts bytecode → machine code
- Works during runtime
- Improves performance by compiling frequently used code
Important note:
- JIT does NOT create JVM or JRE
- It is a part of the JVM
Complete Flow of Java Execution
Now the full process becomes clear:
High-Level Code (Java)
↓
JDK Compiler (javac)
↓
Bytecode (.class file)
↓
JVM (with JIT Compiler)
↓
Machine Code (Binary)
Java vs C++ (Key Difference)
| Feature | C++ | Java |
|---|---|---|
| Compilation | Direct to binary | Bytecode + JVM |
| Platform | Dependent | Independent |
| Execution | Fast | Slightly slower |
| Portability | Limited | High |
Top comments (0)