In our previous article, we learned how to compile and execute our first Java program. Now, let's delve deeper to understand how Java actually converts source code into machine code. The whole process actually contains the compilation process plus runtime processes. Additionally, we will address some frequently asked questions related to the Java compilation process.
What happens in the compilation process?
During the compilation process, the Java source code file
(with the .java extension) is compiled by the Java Compiler. The Java Compiler
converts the human-readable Java code into bytecode, which is a
platform-independent representation of the code. The compiled bytecode is saved
in a file with the .class extension.
The following image describes this process, where a Java file named Simple.java is compiled. After compilation, simple.class file is generated, which is essentially a byte code file.
What happens in the runtime process?
After successfully compiling the source file into bytecode (.class),
the following actions occur at runtime to execute the program. This process takes a bytecode file and converts it into machine code with the help of an interpreter. Following components take part in this Java runtime process.
- Class
Loader: The Class Loader is responsible for loading all the necessary
classes required for the program's execution. These classes are loaded
from various sources, including the hard disk, network, or other
locations.
- Byte
Code Verifier: The JVM (Java Virtual Machine) performs a process
called Byte Code Verification on the compiled bytecode. This verification
ensures that the bytecode adheres to the Java language's format and checks
for any illegal or unsafe code.
- Interpreter: Once the bytecode passes the verification process, the interpreter comes into action. The interpreter is responsible for executing the bytecode line by line. It interprets the bytecode and makes appropriate calls to the underlying hardware to perform the desired operations.
The overall process of executing a Java program can be
summarized as follows:
- Write
the Java source code.
- Compile
the Java code using the Java Compiler, which generates bytecode.
- At
runtime, the ClassLoader loads the necessary classes.
- The
Byte Code Verifier checks the bytecode for format compliance and illegal
code.
- The Interpreter executes the bytecode, line by line, making appropriate hardware calls.
FAQs (Frequently Asked Questions)
Q: What is bytecode?
A: Bytecode is a platform-independent representation of the
Java source code. It is generated during the compilation process and can be
executed by the Java Virtual Machine (JVM).
Q: What is the role of the Class Loader in Java?
A: The Class Loader is responsible for loading the necessary
classes into memory at runtime. It searches for class files in various sources
and makes them available for execution.
Q: Why does Java use an interpreter for executing bytecode?
A: Java uses an interpreter to execute bytecode because
bytecode is platform-independent. The interpreter translates the bytecode into
machine-specific instructions on the fly, allowing Java programs to run on
different platforms.
Q: What is the purpose of bytecode verification?
A: Bytecode verification ensures that the bytecode is
structurally correct, adheres to the Java language's specifications, and does
not contain any illegal or unsafe code. It enhances the security and
reliability of Java programs.
Q: Can a Java program directly execute the source code
without compilation?
A: No, a Java program must be compiled before execution. The compilation process checks for syntax errors and generates bytecode, which is required for the program to run.
This article explains the Java compilation process along with the runtime process. It explains how human-written code is converted into machine-readable code. The compilation process converts .java code to .class code, and runtime converts it into machine-native code. It is important to note that Java uses both a compiler and an interpreter during the Java compilation process.