DEV Community

Cover image for How Java code executes for beginners
Afi0125
Afi0125

Posted on

How Java code executes for beginners

How java exectues for beginnersJava is a high-level programming language that uses a combination of compilation and interpretation to run your code.

Let's deep dive into the procedure:

  1. Writing java code;
    You start by writing your Java code in a text editor or an Integrated Development Environment (IDE). Java code is typically saved with a .java extension.

  2. Compilation:

  • Java Compiler: Once you've written your code, you use the Java compiler (javac) to translate your human-readable code into an intermediate form known as bytecode.Bytecode is a set of instructions that can be executed by the Java Virtual Machine (JVM).

  • Bytecode File: The compiler generates a .class file for each class defined in your Java code. This file contains the bytecode and is platform-independent, meaning it can be executed on any system with a compatible JVM.

  1. Java Virtual Machine (JVM):
  1. Loading: When you want to run a Java program, you use the java command, followed by the name of the class containing the main method (the entry point of your program). For example, java MyProgram.

  2. Class Loading: Once you specify the class to run, the JVM begins by loading the necessary .class files into its memory.

  • These .class files contain the compiled bytecode of your Java classes (the instructions for your program).
  1. Class-Loading Hierarchy:

The JVM follows a specific order or hierarchy when loading classes into memory. This hierarchy ensures that classes are loaded in a controlled and organized manner. There are typically three main stages:

  • Bootstrap Classloader: This is the first step in the hierarchy. It loads core Java classes that are essential for the JVM's operation, like classes from the java.lang package. These classes are critical for the JVM to function properly.

  • Extension Classloader: After the bootstrap classloader, the extension classloader loads classes from Java's standard extension libraries. These libraries provide additional functionality beyond the core Java features. For example, if you use Java's cryptography tools or database connectors, those classes are loaded by the extension classloader.

  • Application Classloader: The final step in the hierarchy is the application classloader. It loads classes specific to your Java application, including the class that contains the main method. This classloader is responsible for loading your program's custom classes and any third-party libraries or JAR files you include.

I'll explain the concept of class loading and the class-loading hierarchy in the Java Virtual Machine (JVM) in a simpler way.

  1. Loading:

When you want to run a Java program, you use the java command, followed by the name of the class containing the main method (the entry point of your program). For example, java MyProgram.

  1. Class Loading:

Once you specify the class to run, the JVM begins by loading the necessary .class files into its memory.
These .class files contain the compiled bytecode of your Java classes (the instructions for your program).

  1. Class-Loading Hierarchy:

The JVM follows a specific order or hierarchy when loading classes into memory. This hierarchy ensures that classes are loaded in a controlled and organized manner. There are typically three main stages:

Bootstrap Classloader: This is the first step in the hierarchy. It loads core Java classes that are essential for the JVM's operation, like classes from the java.lang package. These classes are critical for the JVM to function properly.

Extension Classloader: After the bootstrap classloader, the extension classloader loads classes from Java's standard extension libraries. These libraries provide additional functionality beyond the core Java features. For example, if you use Java's cryptography tools or database connectors, those classes are loaded by the extension classloader.

Application Classloader: The final step in the hierarchy is the application classloader. It loads classes specific to your Java application, including the class that contains the main method. This classloader is responsible for loading your program's custom classes and any third-party libraries or JAR files you include.

In summary, class loading is the process of bringing the necessary Java classes into memory when you run a Java program. The JVM follows a hierarchy to load core classes, extension libraries, and your application-specific classes in a structured and controlled manner to ensure proper program execution.

  1. Bytecode Verification:
  2. Verification: Before executing any bytecode, the JVM performs a verification process to ensure the bytecode is valid and doesn't violate security constraints. This step helps prevent things like buffer overflows and type mismatches.

  3. Just-In-Time (JIT) Compilation:
    Compilation to Native Code: The JVM may employ a Just-In-Time compiler to convert bytecode into native machine code specific to the host system. This step improves execution speed.

Caching: The native code is often cached to avoid recompilation of frequently executed code segments.

  1. Execution:
  2. Execution of main Method: The JVM starts executing your Java program by invoking the main method in your specified class.
  • Execution Flow: As your program runs, the JVM follows the flow of your Java code, executing instructions one by one.
  1. Memory Management:
    • Heap: The JVM manages memory using a heap, which is where objects are allocated and deallocated. The garbage collector periodically frees up memory by reclaiming objects no longer in use.

Stack: The JVM uses a stack for method calls and local variables. Each method call creates a new stack frame, which is popped off when the method returns.

  1. Exception Handling: Java programs can throw exceptions when errors or unexpected conditions occur. The JVM handles these exceptions and can propagate them up the call stack until they are caught and handled by appropriate exception-handling code.

Termination:When your main method finishes executing or an unhandled exception occurs, the program terminates.
The JVM performs cleanup tasks, like finalizing objects, before exiting.

Output and Result:

If your program produces output (e.g., printing to the console), you'll see it on the screen.
If your program returns a value, that value can be used by other programs or scripts calling your Java program.

Top comments (0)