
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the different steps involved to execute a Java program?
Java is an object-oriented programming language that provides various features like platform independence, security, garbage collection, etc. Unlike other programming languages, programs written in Java go through a specific sequence of steps during the compilation and execution process.
Java follows the same process no matter where and how you compile and execute Java programs, whether using an IDE or the Command Prompt. In this article, we will discuss and understand the steps Java follows to execute programs.
Compilation and Execution Process of Java Program
Java program execution follows 5 major steps, which are as follows:
Step 1: Edit or Write Java Program
The very first step is to use a simple editor or IDE to write the Java program. Save the written code with .java extension. Each Java source file may contain one or more class definitions.
Step 2: Compilation
In this step, the source code which has .java extension is converted into bytecode. This bytecode is the language understood by the Java virtual machine. This is what makes Java a platform independent language. Any compile time errors are raised at this step.
The Java compiler (javac) performs the following operations during compilation:
- Converts the source file into tokens and builds a syntax tree (AST).
- Enters class and method symbols into a symbol table.
- If present, processes annotations in the code.
- Resolves names, checks types, and performs constant folding.
- Performs data flow analysis like checking variable initialization.
- Translates syntactic sugar (like enhanced for-loops) into simpler bytecode constructs.
- Creates .class files containing bytecode for each class defined.
Step 3: Load
The program is then loaded into memory. This is done by the ClassLoader which takes the .class files containing the bytecode and stores it in the memory. The .class file can be loaded from your hard disk or from the network as well.
There are two types of class loaders:
-
Primordial ClassLoader: It is the default class loader which is built into the JVM and loads core Java classes.
-
Non-Primordial ClassLoader: Custom user-defined loaders that can control how classes are loaded.
Step 4: Verify
The bytecode verifier checks if the bytecode loaded is valid and does not breach Java security restrictions. If the bytecode does not pass the verification checks, it is rejected, and the class will not be loaded.
Here, following checks are performed:
- All variables are initialized before use.
- Access rules for private members are followed.
- Method calls are type-safe.
- Stack and memory usage are within safe limits.
- Performs data flow analysis like checking variable initialization.
- Local variables are properly indexed.
- Creates .class files containing bytecode for each class defined.
Step 5: Execution
The JVM interprets the program one bytecode at a time and runs the program.
There are two ways JVM executes bytecode:
-
Interpreter: It reads and executes one bytecode instruction at a time.
-
JIT Compiler: It stands for Just-In-Time Compiler. It converts bytecode into machine code at runtime. It avoids repeated interpretation.