How Java Codes Executes

Understanding how a Java program runs behind the scenes is very important. If you're going to learn something, dive deep into it. If you only scratch the surface, you'll miss many things, and this can negatively affect your career. Knowing advanced topics without understanding the fundamentals is like building a big house without a good foundation.

How do Java codes actually execute?

In Java, we save our human-readable code as a .java file, similar to saving C code as a .c file or Python code as a .py file. There's no need to worry; once we finish our program, we save it as a .java file. This is our source code file, which is readable by humans. However, machines can't read this language; they only understand ones and zeros. So, we need to convert it into machine code. This conversion is done using compilers and interpreters. For example, in C, we have our .c file, and we use a C compiler to directly convert it to machine code. However, in Java, there are additional steps between the source file and the machine code process.

Following the image above, you can see that the .java file is converted to a .class (bytecode) file using the Java compiler. Like the C language, we can't run this directly on our system. To run the .class file, we need the JVM. Don't worry about confusing names like JDK, JRE, and JVM; I will explain them below. In summary, there are two steps: first, our source code (.java) is converted to bytecode (.class) by the Java compiler, and then, when it runs via the JVM, it is converted to machine code using an interpreter.

1️⃣ .java ——> compiler ———>.class

2️⃣.class ———> interpreter ————>machine code

This is why we say Java is platform-independent. If we have a .class file, we can run it on any machine. Java is platform-independent, but the JVM is dependent. Don't get confused by this. Look at the image; it helps clarify the concept.

So, in the end, this means our bytecode can run on all operating systems. The section below will help you understand this concept further.

What are JDK, JRE, JVM?

➡️ JDK - Java Development Kit

JDK is a set of Java modules needed for developing and running a Java application. The JDK includes the JAVAC compiler, which is used to compile Java programs. We need the JDK to convert our source code into a format that can be easily executed by the Java Runtime Environment (JRE). If you are a programmer you need this develop the program. this is like a package and it includes,
1. development tools
2. Java compiler
3. archiver - jar
4. interpreter / loader
5. Java doc

➡️ JRE - Java Runtime Environment

This is an installation package that provides an environment to run Java programs. You cannot create Java programs with it; you can only run them. it consists of.
1. Deploy technologies
2. User interface toolkits
3. Intergration libraries
4. Base Libraries
5. JVM

➡️JVM - Java Virtual Machine

Java Virtual Machine is a program that runs pre-compiled Java programs. This means the JVM converts each bytecode (.class files) instruction into machine language instructions that the microprocessor can execute.
How JVM works internally?
Class loader —> Byte code verifier —→ Interpreter —→ Runtime —→ Hardware

◽Loading
In this process, the .class file is read to generate binary data and create an object for the relevant class in the heap.

◽Linking
👉🏻 JVM Verifies the .class file (checks for bytecode validity, security, and structural correctness before execution).
👉🏻 Allocates memory for class variables & assigns default values.
👉🏻 Replaces symbolic references with direct memory addresses for faster execution.

◽Initialization
👉🏻 All static variables are assigned with their values defined in the code and static block.

I hope this article helped you understand the core concepts of JVM, JRE, and JDK. If anything is unclear or if I’ve made any mistakes, feel free to share your thoughts in the comments. Your feedback is always welcome, and let’s keep learning together💖!

0
Subscribe to my newsletter

Read articles from Harshana Prabhath directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Harshana Prabhath
Harshana Prabhath