Java bytecode is code that is generated by the Java compiler javac from the Java source code. The Java Virtual Machine (JVM) executes this bytecode, which is stored with in .class files.
Why the Need for Java Bytecode?
Computers cannot run source code directly. A compiler converts it into instructions the computer can execute. To enable the computer to understand it, programming languages convert source code into machine-readable code in 0’s and 1’s. Programming languages like C generate native machine code that is compatible with the underlying processor architecture. The drawback of such machine code is that code compiled on one computer is not compatible with other computers, so programmers must compile it again. For example, if a programmer writes a program on a computer that has the Windows operating system, the native machine code is in an .exe file. This .exe file is compatible only with Windows and cannot be run on operating systems like Linux or macOS, because those systems use different executable formats: ELF on Linux and Mach-O on macOS.

However, programming languages that generate Java bytecode use bytecode. This bytecode is independent of the underlying architecture or operating system (such as Windows, macOS, or Linux) due to the JVM. As a result, programmers can copy the compiled .class file to another computer and run it without recompiling.
Limitations of Executable Files
Executable files have some limitations, particularly when an application needs to run on different hardware platforms and operating systems. Some of the limitations are as follows,
Platform Dependence:
Executable files are compatible with only a single operating system. As a result, an executable file generated for Windows cannot run on another operating system, such as Linux or macOS. For example, the executable file for Windows is PE (.exe), Linux (ELF), and Mach-O for mac OS.
Processor Architecture
Another issue with executable files is that they don’t support different architectures. Each architecture differs in the type of machine instructions, making machine code from one processor non-executable on another.
Due to these limitations, native executable files cannot run across different OS and processor architectures.
How Does Java Programming Address the Limitations of Executable Files?
Instead of generating the native machine code, the Java compiler (javac) generates bytecode by converting the source code. This bytecode with a .class extension is stored in a file. Importantly, this bytecode is not specific to a single operating system or architecture but is a common instruction format that the JVM can easily understand. As a result, after writing and compiling the Java program on a computer with a Windows-based OS, the programmer can transfer the .class file and run it on different OSs like Linux or macOS without needing to recompile the Java source code. This makes Java programs popularly known as “Write Once, Run Anywhere”. Thus, Java overcomes the limitations of native executable files.
What are the Steps in Generating the Java Bytecode?
Compiling is a single command, javac, but the full process has three steps: write, compile, and run. in which the Java compiler (javac) generates bytecode. For example, if a programmer is writing the Java code with a display as Ford Car Started, then it involves the following steps:
The First Step: Writing the Java Source Code:
Before writing the code, name the file FordCar.java.
class FordCar { public static void main(String[] args) { System.out.println("Ford car started!"); }}
This is the typical Java code format that a programmer should follow.
In the second step, the programmer should compile the Java program.
As a result, the Java compiler (javac) checks the source code for compilation errors and, if there are none, creates the bytecode file FordCar.class:
javac FordCar.java
However, this is not the final code; it is an intermediate form that enables the JVM to understand and execute it. Important Note: In Java, the compiler (javac) is responsible for generating bytecode, not the JVM.
Finally, as a third step, the JVM loads and runs the bytecode:
java FordCar
What are the Advantages of Java Bytecode?
Because of bytecode generation, Java exhibits several advantages, particularly over programming languages that generate native machine code. They include the following,
1) Platform Independence: the same bytecode runs on any system with a JVM. Portability: you can distribute .class or .jar files without sharing source code.
2) Portability: It enables programmers to distribute the bytecode from one computer to another and successfully run it without any issues even if the other computer features a different operating system.
3) Performance Optimization: The JVM converts the bytecode into native machine code using the JIT compiler, enabling higher performance.
4) Security: Along with compatibility, Java bytecode assures security because the JVM runs the code only if the bytecode verifier finds that it follows Java’s safety rules.
Programming Language Examples That Generate Bytecode and Native Machine Code
Programming languages do not follow a single procedure when translating source code into machine code that enables computers to execute instructions.
Below is the sample list of programming languages that follow different methods,
Native Machine Code: C, C++, Rust, Go, and Swift are common examples that generate native machine code compatible with a specific platform or operating system.
Bytecode: While Java generates Java bytecode, Python generates Python bytecode. Likewise, C# generates Common Intermediate Language (CIL).
Other Languages that Deploy JVM: Scala, Kotlin, and Groovy also generate Java Bytecode for the JVM to execute.
Check the Interview Questions on JVM page for more clarity of the subject.