#$ Java Compilation Process
Java is a platform-independent programming language developed with the philosophy of "Write Once, Run Anywhere." This feature is made possible by Java's compilation and execution process. In this article, we will examine the stages a Java program goes through from source code to execution.
#1. Writing the Source Code
Java programs are written in files with the .java
extension. These files contain human-readable source code written by the developer.
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
#2. Compilation with the Compiler (javac)
Java source code is compiled using the Java Compiler (javac
). As a result of the compilation process, the source code is not converted directly into machine code; instead, bytecode is generated, which can be executed by the Java Virtual Machine (JVM). Bytecode is stored in files with the .class
extension.
javac HelloWorld.java
When this command is executed, a HelloWorld.class
file is created in the same directory.
#3. Bytecode and Platform Independence
The generated bytecode is not specific to any operating system or hardware. It can be executed on any environment where a JVM is available. This is what makes Java programs platform-independent.
#4. Execution with the Java Virtual Machine (JVM)
The compiled .class
file is executed by the JVM. The JVM takes the bytecode and translates it into the machine code of the platform it is running on, then executes the program.
java HelloWorld
This command prints the output of the program to the screen:
Hello, World!
#5. Summary of the Compilation and Execution Process
- Source code is written (
.java
file). - Compiler translates it into bytecode (
.class
file). - JVM executes the bytecode.
#6. Extra: JIT Compiler
While running the program, the JVM uses the Just-In-Time (JIT) compiler to convert bytecode into machine code at runtime. This improves the performance of the program.
#Conclusion
Java's compilation process enables the development of platform-independent, secure, and efficient applications. Thanks to this process, Java remains one of the most popular programming languages today.