How Java Works: A Beginner-Friendly Guide

When I started learning Java, one of the first questions I had was "How does Java actually work under the hood?" Sure, we write code in .java files and somehow it runs—but what’s happening behind the scenes? In this blog, I’m going to break it all down from source code to execution, including compilers, JVM, memory areas, and even JIT optimizations.



1. Writing the Code

Everything begins with writing Java code in a file with a .java extension. For example:

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

This is our source code—readable by humans, not machines.


2. Compilation with javac

Java uses a compiler called javac. When we run:

javac Hello.java

It converts the source code into a .class file containing bytecode—a platform-independent, intermediate code that the Java Virtual Machine (JVM) can understand.


3. Enter the JVM (Java Virtual Machine)

The JVM is the engine that runs Java programs. It doesn’t understand .java files directly, only the compiled .class files with bytecode.

Key roles of the JVM include:

  • Loading classes

  • Verifying bytecode

  • Executing code

  • Managing memory (via garbage collection)


4. Class Loader

The Class Loader is part of the JVM. It loads classes into memory as needed. There are three main types:

  • Bootstrap ClassLoader: Loads core Java classes (java.lang.* etc.)

  • Extension ClassLoader: Loads optional libraries.

  • Application ClassLoader: Loads classes from your app's classpath.


5. Bytecode Verifier

Once classes are loaded, the JVM’s Bytecode Verifier checks them for safety:

  • Ensures no memory corruption

  • Confirms type safety

  • Verifies proper access levels

This makes Java more secure and robust.


6. Memory Areas in JVM

The JVM organizes memory into several key areas:

  • Method Area: Stores class-level metadata like method definitions and static variables.

  • Heap: Stores all Java objects. This is managed by the garbage collector.

  • Stack: Each thread has its own stack for local variables and method calls.

  • PC Register: Keeps track of the current instruction being executed for each thread.

  • Native Method Stack: Used when Java calls native (non-Java) code, like C/C++.


7. Interpreter vs. JIT Compiler

The JVM has two main ways to run bytecode:

  • Interpreter: Executes code line-by-line. It’s fast to start but slower for long-running code.

  • JIT (Just-In-Time) Compiler: Detects frequently used code (“hot spots”) and compiles it into native machine code at runtime for better performance.


8. HotSpot JVM

Java’s most widely used JVM is the HotSpot JVM. It includes:

  • Advanced JIT Compilation

  • Hot spot detection (hence the name)

  • Efficient garbage collection

  • Optimizations for long-running applications

HotSpot is smart. It watches how your program behaves, then adapts and optimizes for performance over time.


9. JVM Language Stack

It’s not just Java that runs on the JVM. Other languages like Kotlin, Scala, and Groovy also compile down to bytecode and run on the JVM. That’s why we call it the JVM language stack—a whole ecosystem built on the same powerful foundation.


Conclusion

Understanding how Java works internally—from source code to bytecode, from class loading to JIT optimization—has helped me appreciate the language even more. It’s not just about writing System.out.println(), but about knowing what happens behind the scenes.

If you're new to Java, I hope this breakdown helps you as much as it helped me. Keep learning—Java is a deep and rewarding language!


Here’s a visual breakdown of how Java works—from writing code to execution through the compiler, JVM, interpreter, JIT, and HotSpot.

Generated image

0
Subscribe to my newsletter

Read articles from ADARSH KUMAR PANDEY directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ADARSH KUMAR PANDEY
ADARSH KUMAR PANDEY