Bytecode is an intermediate, platform-independent code generated when a .java file is compiled into a .class file. This bytecode is executed by the Java Virtual Machine (JVM), enabling Java’s Write Once, Run Anywhere principle.
- Bytecode consists of instructions meant for the JVM, not for any specific hardware or operating system.
- It ensures platform independence, allowing the same program to run on different systems.
- Bytecode is verified by the JVM for security before execution.
- It supports efficient execution through interpretation and JIT (Just-In-Time) compilation.
How is Byte Code generated?
The execution of a Java program happens in the following steps:
- Source Code: The programmer writes code in a high-level language (Java).
- Compilation: The Java compiler (javac) converts the source code into bytecode (.class file).
- Execution by JVM: The JVM loads and executes the byte code using an interpreter and Just-In-Time (JIT) compiler.
Byte code acts as a bridge between the compiler and the JVM, making Java platform-independent.

Java code is first compiled into bytecode and then converted into machine code by the JVM during execution, which is illustrated in picture above.
Step-by-Step Illustration
Consider the following Java program:
import java.io.*;
class GFG {
public static void main(String[] args) {
System.out.println("GFG!");
}
}
Output
GFG!
Explanation:
- The above code is Java source code (.java file).
- The Java compiler converts it into byte code (.class file).
- The JVM executes the byte code and produces the output.
Why Byte Code Makes Java Platform-Independent
- Java programs are not directly converted into machine code of a specific operating system.
- Instead, they are converted into byte code, which is independent of hardware and OS.
- Any system with a compatible JVM can execute the same byte code.
This is the reason Java follows the principle: "Write Once, Run Anywhere (WORA)"
Note:
- Byte code is not machine code; it is executed by the JVM.
- The JVM may interpret byte code or compile it at runtime using JIT (Just-In-Time) compilation for better performance.
- Different platforms have different JVM implementations, but they all understand the same byte code.