DEV Community

Subash
Subash

Posted on

Java Architecture – How Java Works?

1. Introduction

When I started learning Java, I came across a lot of terms like JDK, JRE, JVM, bytecode, and class files. Initially, it was a little confusing to understand how all these things work together.

So, I decided to write a simple blog explaining what Java is and how its architecture works. I'm sharing what I have learned so far, and I hope this will help other beginners too.

2. What Is Java?

Java is a high-level, object-oriented, general-purpose programming language developed by Sun Microsystems. It is now owned and maintained by Oracle.

Java is widely used to develop web applications, enterprise applications, desktop software, and many other types of applications.

One of the main reasons Java is popular is its platform independence.

We often hear the phrase "Write Once, Run Anywhere" (WORA). It means that we can compile Java code once and run the generated bytecode on different operating systems, provided they have a compatible Java Virtual Machine (JVM).

3. Java Architecture

To understand Java architecture, we need to understand how Java code gets executed.

When we write a Java program, it doesn't directly become machine code. First, it is compiled into bytecode, which is then executed by the JVM.

How Does It Work?

Let's understand the process step by step.

Step 1: Write the Java code

First, we write our program in a file with the .java extension.

For example, Hello.java.

This file contains the source code that we write and understand.

Step 2: Compile the code

The Java compiler, called javac, compiles the source code and generates a .class file.

For example, Hello.class.

This file contains bytecode rather than ordinary Java source code.

Step 3: Load the class

The JVM's Class Loader loads the required class files into memory so that the program can be executed.

Step 4: Verify the bytecode

The Bytecode Verifier checks whether the bytecode follows the required rules and is structurally valid.

Step 5: Execute the bytecode

The JVM executes the bytecode using its execution engine. Depending on the implementation, this can involve interpretation and Just-In-Time (JIT) compilation into native machine code.

That's how our Java program gets executed!

4. Components of Java Architecture

There are three important terms we need to understand: JDK, JRE, and JVM.

4.1 JDK (Java Development Kit)

JDK stands for Java Development Kit.

It provides the tools required to develop Java applications. It includes the Java runtime components along with development tools such as:

  • javac – Compiles Java source code.
  • javadoc – Generates documentation from Java source code.
  • jdb – A command-line debugger.

In simple words, if we want to develop Java applications, we need a JDK.

4.2 JRE (Java Runtime Environment)

JRE stands for Java Runtime Environment.

It provides the runtime components and libraries required to run Java applications. In the traditional Java distribution model, it includes the JVM and supporting libraries.

For example, Java provides ready-to-use classes such as String and ArrayList, which we can use in our programs instead of implementing everything from scratch.

In simple words, the JRE provides the environment needed to run Java applications.

Note: Modern Java releases do not always provide a separate JRE download. The runtime can also be packaged using tools such as jlink.

4.3 JVM (Java Virtual Machine)

JVM stands for Java Virtual Machine.

It is responsible for executing Java bytecode. It also helps make Java platform-independent.

Some of its important components and responsibilities are:

  • Class Loader: Loads the required class files.
  • Bytecode Verifier: Checks bytecode validity and safety constraints.
  • Execution Engine: Executes bytecode using interpretation and JIT compilation.
  • Memory Management: Manages runtime memory, including garbage collection.

The JVM is platform-specific, but Java bytecode can run on any compatible system that provides an appropriate JVM.

5. Difference Between JDK, JRE, and JVM

Here is a simple way to remember the difference:

  • JDK: Used to develop Java applications.
  • JRE: Provides the environment and libraries needed to run Java applications.
  • JVM: Executes Java bytecode.

Think of it this way: the JDK provides development tools, the runtime provides what the application needs to run, and the JVM executes the bytecode.

6. Summary

Let's quickly revise what we learned.

  1. Java is a high-level, object-oriented programming language.
  2. Java source code is saved in a .java file.
  3. The javac compiler converts source code into bytecode stored in a .class file.
  4. The JVM loads and executes the bytecode.
  5. The JDK provides development tools, while the runtime provides the components needed to run Java applications.
  6. Java supports platform independence because compatible JVM implementations can execute the same bytecode on different operating systems.

These are the basics of Java and its architecture. Understanding these concepts makes it easier to learn more advanced Java topics.

I'm still learning Java, and I'll keep sharing my notes as I progress. If you find anything that needs correction or have a suggestion, feel free to share it in the comments!

Thanks for reading!

Top comments (0)