Java Execution Flow
- Java execution flow describes how a program runs from the moment it’s written to its execution. Understanding this flow is crucial for efficient debugging and program optimization.
1.Coding
- Coding is nothing but writing a Java program and saving that file with a
.java
extension. - The
.java
file is also known as a source file because generated class file source file will act like a source. - The file name should be same as the Class name.
2. Compiling
- Compilation is nothing but converting a one language into another language.
- To invoke a compiler, we need to make a use of command -> Javac FileName.Java
- Once we in compiler, it consist of Java standard library and algorithm.
- In Java standard Library, there are set of rules. We need to followed by programmer in order to compile source file.
- Compiler algorithm is responsible to check our code line by line.
- The file will be saved in a
.class
file extension.The.class
file will be same as a Java class file - Command: Use the
javac
command to compile your code: - The output is a
.class
file, which contains the bytecode (e.g.,MyProgram.class
).
3. Class Loading
- Class loading means bringing a
.class
into JVM memory and Class loading happens only once. - Class loading brings only
.class
file - The ClassLoader in the Java Virtual Machine (JVM) loads the
.class
files into memory. - Types of Class Loaders:
- Bootstrap ClassLoader: Loads core Java classes (e.g.,
java.lang
). - Extension ClassLoader: Loads extension libraries.
- Application ClassLoader: Loads user-defined classes.
- Bootstrap ClassLoader: Loads core Java classes (e.g.,
4. Bytecode Checker
- The job of Bytecode Checker is used to check whether the
.class
file is corrupted or not. If.class
file is corrupted, then it stop the execution. - The JVM verifies the bytecode to ensure it adheres to Java’s security and execution rules.
- This step protects against:
- Memory corruption.
- Malicious code execution.
5. Execution of Main Method
- To invoke execution, we need to make use of command Java FileName.
- Once we invoke execution, it will be done with the help of JRE
- JRE consist of powerful mechanism called JVM.
- JVM stands for a Java virtual machine. JVM is also called as powerful mechanic mechanism because it consist of its own architecture, memory area and functional units.
- After successful verification, the main() method is executed. This is the entry point of any Java program.
- The method signature is always
public static void main(String[] args)
and is required for the program to run.
6. Interpreter/Just-In-Time Compilation (JIT)
- The Java Interpreter begins executing bytecode line by line. However, to optimize performance, the JVM uses JIT (Just-In-Time) Compilation.
- JIT compiles frequently executed code into native machine code while the program runs, improving performance.

7. Memory Management
- Java Virtual Machine (JVM) has a special mechanism of automatic garbage collection that is responsible for taking care of memory by removing the objects that are no longer used.
- We create objects on heap memory, and we delete them when we avoid them.
8. Program Termination
- The program terminates when the
main()
method completes execution, or if an exception is thrown and uncaught. - After the program ends, the JVM releases all resources, and the process exits.
9. Output Generation
- During execution, the program may produce output (e.g., printing to the console).
- The output is displayed through the standard output stream (System.out).
10. Error Handling and Exception Flow
- If an error occurs during execution, the JVM looks for a corresponding exception handler.
- Exceptions, also known as runtime errors, occur when an issue arises, such as division by zero or a null pointer reference.
- If an exception is raised and remains uncaught, the program will terminate unexpectedly.
11. Finalization
- Upon the completion of the program’s execution, the JVM may invoke finalizers for any objects requiring specific cleanup procedures, such as the closure of file streams.
- Finalization happens before the program exits, freeing resources.
Summary of Java Execution Flow:
- Write the code (
.java
file) - Compile the code to bytecode (
.class
file) - Class loading into JVM using ClassLoader
- Bytecode verification for security
- Execution of
main()
method - Interpreter or JIT compilation for optimization
- Method calls executed in sequence
- Memory management and garbage collection
- Program termination after
main()
method ends - Error handling with exceptions
- Finalization of resources