Java

How does Java get compiled

Java is an object-oriented programming language that has gained popularity over the years. Java is used to build multiple software. It gets compiled over a series of steps. The compilation, in general, is a procedure that converts the source code into a code understandable by the computer, which is mostly referred to as machine code. This article discusses the compilation of Java in detail.

Pre-requisites

The pre-requisites for executing a Java program are:

1. Install and set up a Java platform that comprises Java Runtime Environment (JRE) and Java Development Kit (JDK).

2. A text editor of your preference such as NetBeans, Eclipse, Oracle, etc.

Compilation and Execution of Java

As already mentioned java program compiles over a series of steps. The java code is first converted into bytecode rather than being compiled into executable files. As a result, a machine code is produced which is transferred to the memory by the Java Virtual Machine (JVM). The compilation and execution procedure for Java is explained in detail below.

Compilation Time

The Java compiler compiles the Java file at compilation time and as a result, the Java source code gets converted to bytecode. Here is a pictorial representation of the compilation procedure.

Java Code

(Simple.java)

Byte Code

(Simple.class)

Execution Time

At runtime, the class file is fed to the classloader which is a subsystem of JVM responsible for loading classes. Afterward, the bytecode verifier evaluates the code for any sort of illegality that might breach access rights to objects. Finally, the interpreter reads the bytecode stream and executes the instructions. The whole execution procedure is shown below.

Conclusion

Java which is an object-oriented programming language compiles over a series of steps. The first step is the compilation step in which, the java file is compiled by the java compiler as a result of which the java source code transforms to bytecode. Meanwhile, at the second step which is the execution step, the class file is fed to the classloader and the bytecode gets verified by the bytecode verifier. After verification, the bytecode is read into the stream and the instructions are executed. This tutorial demonstrates the java compilation procedure with the help of visual representations.

About the author

Naima Aftab

I am a software engineering professional with a profound interest in writing. I am pursuing technical writing as my full-time career and sharing my knowledge through my words.