Let me explain you how exactly things work
Let’s start with this simple Java file:
Now, let’s see how this gets executed under the hood
You write Java code — The Source Code →
1 )You write your program and save it as [Link].
2) This code is human-readable and contains Java syntax.
File: [Link]
JDK (Java Development Kit) →
•The JDK is the complete package for Java development.
•It contains:
• javac → Java compiler
• java → Java launcher
• JRE (Java Runtime Environment)
• Development tools (debugger, Javadoc, etc.)
JDK = JRE + Development Tools
So when you run javac [Link], it’s the JDK’s compiler doing the work.
javac — The Compiler →
•The Java Compiler (javac) converts your .java source code
into bytecode.
•Bytecode is platform-independent and saved in a .class
file.
Command: Output:
javac [Link] [Link] ← (Contains Java Bytecode)
Bytecode — The Platform-Independent Code →
•This .class file doesn’t contain machine code.
•It contains bytecode, a set of instructions understood by the JVM.
•That’s why Java is “Write Once, Run Anywhere.”
JRE (Java Runtime Environment) →
•The JRE provides everything needed to run a Java program
(not develop it).
•It contains:
• JVM (Java Virtual Machine)
• Core libraries (Java API classes)
• Runtime components
JRE = JVM + Libraries
So, if you only want to run Java programs, you just need JRE.
JVM (Java Virtual Machine) →
•The JVM is the heart of Java — it’s what actually executes your
code.
•It takes the bytecode and translates it into machine-specific
instructions.
•The JVM performs:
• Class Loader → Loads .class files into memory.
• Bytecode Verifier → Checks for security and validity.
• Interpreter / JIT Compiler → Executes bytecode
efficiently.
• Garbage Collector → Manages memory automatically.
Command:
java Hello
JVM interprets or JIT-compiles your bytecode into native machine code → executed by CPU.
Enough of the theory part now I will explain
you with an example how exactly things work
Go to notepad or notepad ++ and write this java program and save it in a specific folder with .java extension
Now go to the specific location where you have saved your java file and in the path bar type cmd (it will open command
prompt)
Now type javac [Link] which is your saved file name ,you can press tab after typing Test it will automatically take it and
hit enter
Now the compiled bytecode with .class extension will be created in the specific location. This is something which
humans can’t understand.
Now go back to the command prompt and type java Test , then your output will be visible.
Q1) So why java and not javac ?
The java command is used to launch the JVM, which then interprets and executes the compiled bytecode line by line.
Q2) So why are we giving only Test and not [Link] ?
JVM will automatically take the compiled version which is the .class (bytecode) file hence it’s not required to give .class ,
If we will do so it will throw error .
What is an Interpreter ?
The Interpreter in the JVM is responsible for executing bytecode line by line.
When the JVM receives the compiled .class (bytecode) file, it first parses and analyzes the instructions to understand their
execution order. Once organized, the Interpreter reads and executes each instruction sequentially, one at a time.
In simple terms, the Interpreter translates bytecode into machine code line by line at runtime, ensuring smooth and
immediate execution of Java programs.
What is JIT (Just-In-Time Compiler) ?
The JIT compiler is a part of the JVM that improves performance by compiling frequently executed sections of bytecode
into native machine code at runtime. If a block of code is used repeatedly, JIT compiles it once and stores the optimized
version, allowing the JVM to execute it directly without reinterpreting it every time — thus saving time and enhancing
speed.