Execution Process of Java Code

Execution Process of Java Code

Blog-2 of "Get Started with Java" series.

·

4 min read

Welcome🎉 to another blog of the "Get Started with Java" series. In this blog, we will see the Execution process of Java code.

Before we dive into the Execution process, let's see some important terminology in brief:-

JDK (Java Development Kit)

Java Development Kit (JDK) is a software development environment used for developing Java applications and applets.

It includes the Java Runtime Environment (JRE), an interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc), and other tools needed in Java development.

JRE (Java Runtime Environment)

It may also be written as “Java RTE". It provides the minimum requirements for executing a Java application.

it consists of the Java Virtual Machine (JVM), core classes, and supporting files.

JVM (Java Virtual Machine)

The JVM is the specification for a software program that executes code and provides the runtime environment for that code.

In simple terms, The JVM is how we run our Java programs. We configure the settings and then rely on the JVM to manage program resources during execution.

More on JVM Here.

The Secret of Java- JDK, JRE, JVM difference – Mann Verma – Medium

Detailed image of JDK.

Ref:- http://www.jtech.ua.es/j2ee/restringido/documents/jdk-6u21/index.html

Execution Process:-

Now we will see the execution process of the Java program.
The execution process has the following steps:-

  1. Writing and Compiling Java Code:

  2. Compilation:

  3. Java Virtual Machine (JVM)

  4. Class Loading

  5. Execution

  6. Output

  7. Program Termination

We will see this whole process step by step...

  1. Writing and Compiling Java Code:

  • To execute a Java program, we first need to write the code using a text editor or an Integrated Development Environment (IDE). Let's start with a simple example:

      // HelloWorld.java
      public class HelloWorld {
          public static void main(String[] args) {
              System.out.println("Hello, World!");
          }
      }
    

In this Code, we have created a HelloWorld.java file and in that file we have written some code, If you are beginner you won't understand the code for now.

This code just prints "Hello, World" in the terminal. So we expect output to print "Hello, World" in our output screen.

  1. Compilation
  • After writing the Java code, the next step is to compile it. Java source code is saved with the extension ".java," and it needs to be converted into bytecode that the Java Virtual Machine (JVM) can understand.

  • Java Compiler does this work.

  • In the command line, navigate to the directory where the Java file is saved and execute the following command:

javac HelloWorld.java
  • If there are no errors in the code, this command will generate a bytecode file named "HelloWorld.class".
  1. Java Virtual Machine (JVM):
  • The JVM is a crucial component of the Java execution process. It acts as an interpreter and executes the bytecode produced by the compiler.

  • The JVM is responsible for platform independence, memory management, and security. It provides an execution environment for Java programs.

  1. Class Loading:
  • Before the JVM can execute a Java program, it needs to load the necessary classes.

  • The class loader locates and loads the bytecode files.

  • When the JVM encounters the "java" command, it loads the main class specified in the command line.

  1. Execution:
  • Once the main class is loaded, the JVM starts executing the program from the main method. The main method serves as the entry point of a Java program. It has the following signature:

    public static void main(String[] args)

  • In our example, the main method of the HelloWorld class is executed:

      public static void main(String[] args) {
          System.out.println("Hello, World!");
      }
    
  1. Output:
  • During execution, the System.out.println() statement is encountered. It prints the string "Hello, World!" to the console. This is the output we see when we run the program.
  1. Program Termination:
  • After the execution of the main method completes, the Java program terminates.

Tada... You learned how the Java program is being executed. 🎉

Conclusion:

Understanding the Java execution process is vital for every Java programmer.

In this blog, I covered the essential steps involved in executing a Java program, from writing and compiling code to the role of the JVM in executing bytecode.

By following this guide, beginners can develop a solid foundation and enhance their Java programming skills.

Thanks for reading. 😊

Â