How JAVA program is COMPILED and EXECUTED?

How JAVA program is COMPILED and EXECUTED?

Table of contents

No heading

No headings in the article.

All other programming languages like c++ or c have the same compilation method

They work in 2 steps, where source code is written and saved in a file then the file is compiled and the code is converted to machine code.

But here comes java which has 3 steps of compilation, yes it has one more step extra, and the extra step is to make java efficient and the platform independent.

So, excited to know about the java compilation and what's this platform independency is and all stuff.

let's dive into it.

kindpng_1746636.png

Here comes the first step,

The source code is written and stored in a file with an extension of .JAVA

This is source code and it's human readable.

Now, this needs to be converted to binary so the machine can understand and execute it.

But, The source code is converted to byte code through the javac compiler with the extension of .class

java_to_bytecode.jpg

Now u may ask why convert to byte code. why not directly to machine code?

This byte code helps in platform independency.

It simply means that once the source code is compiled to byte code it doesn't need to be compiled again for another platform.

For example,

If u write a program in CPP and compile and run it in windows

And if I want to run the same program you have written in mac, then I need to recompile it and run it. It is known as platform independent.

But in java, once the code is converted to byte code it can run on any system with JVM, with no need for recompilation.

Now. Class file is sent to JVM for converting to machine code.

class_to_binary.jpg

"Java is platform independent but JVM is not."

Let's have a look at what is JVM and what happens inside the JVM

JVM- JAVA VIRTUAL MACHINE it is a machine which used to run java programs. It consists of a class loader, verifier,JIT, ..ETC

what happens inside the JVM?

  1. Class loader :- loads all classes needed for the program
  2. VERIFIER:- verify the byte code SYNTAX.
  3. Interpreter:- the interpreter execute the code line by line

Finally, the byte code is converted to binary code. Now it runs in JRE.

SOURCE CODE --> BYTE CODE --> BINARY CODE

that's the whole process of compilation and execution of a java program.

Let's talk about things required to run java on a machine

  1. We need a java development kit(JDK)

You can get it here;- oracle.com/in/java/technologies/javase-down..

  1. WE need INTELiJ

You can get this here:-

jetbrains.com/idea/download/#section=windows

You can install these and two and run your java programs on your machines.

thanks for reading.

For more computer science-related stuff. Follow me

TWITTER