How Java Code executes


Hello to Readers !! If you are a Java Developer, you may not have noticed that how Java code compile and run, or you may not have a good understanding of the overall process because of its complexity. In this article I will explain you how java code executes and overview of Java Architecture.
Java Compilation Process-
.java file
.java file contains the source code . We will write our code in .java file like Sum of two numbers ,Hello World, Java Application etc.
We need to convert source code to machine code so computer can understand.
Compiler — In C , C++ programming languages , the role of compiler is take the source file and convert it into the machine readable format for ex. 0's and 1's.
But in Java , compiler/loader compiles the entire .java file into byte code and the extension of that byte code is .class .
.class file
Byte Code — Some intermediate language of Java .This code will not directly run on a system. We need JVM( Java Virtual Machine) to run this code.
Command to compile the code — javac filename.java
Command to run the code — java filename
Machine Code ( set of instructions for the computer)
Interpreter — Executes the code line by line .
JVM converts byte code into machine code line by line .Here JVM works as Interpreter. .class file can be run on any operating system. This is the reason why Java is platform independent.
More about platform independent — It means that byte code can run on all operating systems.
Java Architecture (JDK vs JRE vs JVM vs JIT)
JDK ( Java Development Kit) —
Provides environment to develop and run the Java program.
JDK consists JRE — to execute Java program and Development Tools — to provide an environment to develop Java program.
JRE ( Java Runtime Environment) —
It is an installation package that provides environment to only run the program.
It consists 1. Development technologies 2. User interface toolkit
3. Integration libraries 4. Base libraries 5. JVM
After we get the .class file ,the next things happen at run time:
1. Class loader loads all classes needed to execute the program.
- JVM send code to byte code verifier to check the format of code.
JVM —
JVM works like a interpreter . Execute the program line by line.
When one method is called many times ,it will interpret again and again.
JVM contains the Heap and Stack memory allocation and allocates memory to class variables and default values.
JIT —
Those methods that are repeated, JIT provides direct machine code so
re-interpretation is not required. Makes execution faster.
Subscribe to my newsletter
Read articles from Srushti directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
