The Java Program Execution Procedure

The Mystery of Java Program Execution

By ANONYO SANYAL
Now, this blog is in JAVA programming language, probably the most used programming language in real life scenarios. Java is an Object Oriented Programming Language which makes it perfect for the development of applications. It finds it's application in small music players to the android application that you run in your smartphones. To know more about Java please go through these

You might know very well the hello world program in java. If not, it is given below.

class Simple{
 public static void main(String args[]){
 System.out.println("Hello World");
 }
 }

The output of this code is the message "Hello World".
Now the question is how the heck is it executed.

What happens when we compile the code?

During compilation, the code is fed to the compiler which converts the code into instructions which the computer can understand. In Java, the machine code is known as Bytecode. Now here we must note that we store the java program in our computer with a .java extension. But as soon as the code is compiled an extra file is created in the same folder where our program was stored. It has an extension .class at the end of it. Pretty confused eh? Let's go back to basics!

Now we all are aware of the fact that computers only understand combinations 1's and 0's. Our language is alien to it. For example, when you type A in your keyboard, does your CPU understand it's A? No, it interprets it as 01000001. Believe it or not, it has no idea that you meant A. But the computers are designed in such a way that when you type something there are some internal translators who convert the message that you are familiar to into the language that your CPU is familiar to i.e. 1's and 0's. But mind you the message is unchanged. Just the language has changed. 

Similarly, the java compiler also acts as a translator. It translates your java program which is easy for you to write into a bytecode which the machine understands.
Now as shown in the diagram the java compiler acts as a translator which translates the java code into a Bytecode. Now the Bytecode is executed. As mentioned earlier the instructions that you gave in the java code is not changed, rather they are converted to bytecode keeping the instructions same. So you don't have to worry about the behavior of your program. The way you give the instructions the same way the code will get executed. It's like saying "Buy me an apple" in Chinese. The language is different but the message or outcome is the same, i.e buying the apple for you.

Then what happens after the code is compiled?

Straight answer, executed. But only if you ask it to do so. If you just ask to compile the code, then it will just be compiled i.e. the appropriate bytecode will be generated. It won't be executed. To execute you need to give a command to run the program. The commands are as follows:-
  • To compile -> java filename.java
  • To run -> javac class name
At run-time(when the program runs) the following steps take place.

 Though we say run-time there are a few steps that take place before run-time arrives. The first and foremost is the class file containing the bytecode is loaded with the help of class-loader. The class loader is a subsystem of Java Virtual Machine(JVM) that is used to load class files to the memory. Then the Byte-code verifier checks the code fragments for any illegal piece of code that may violate access rights of objects(for security purposes). After that the interpreter reads the bytecode stream then executes the instruction and thus we arrive the run-time. During run-time, the hardware is used i.e the CPU. At the end of it, the desired result is achieved which can be perceived in hardware components.

This is how the java code is executed. Comment below if you have any doubts.

Comments

Popular posts from this blog

What is Artificial Intelligence(AI)?

Different Addressing Modes used in Addressing