ThinkSphereEdu

How Java Works: A Beginner’s Best Guide to Understanding JVM, JRE, and JDK 2025-26

How Java Works: A Beginner’s Best Guide to Understanding JVM, JRE, and JDK 2025-26

How Java Works is one of the most common questions every beginner has when starting their programming journey. Java is more than just a popular programming language – it is a complete platform that ensures your code runs smoothly on any machine, regardless of the operating system. But how does this magic actually happen? The secret lies in the powerful trio of JVM (Java Virtual Machine), JRE (Java Runtime Environment), and JDK (Java Development Kit).

In this article, we (ThinkSphereEdu.com) discuss a comprehensive overview of how java works? And trace the journey of a Java program fom its initial source code form to the final output.

How Java Works: From Source Code to Output

The first step of the Java execution process is to write our first Java code itself. To write our first Java code, we need to install any IDE (Integrated Development Environment). In this case I prefer VS Code as the best IDE for programming.

This involves creating a file with the .java extension, like FirstProgram.java. This file contains the source code, which contains class definitions, methods, variables, and other Java language constructs.

The Java code is written in a .java file, which is not directly executable by the computer processor. At first it needs to be compiled into an intermediate representation called bytecode. This is where the Java compiler is used.

The command javac FirstProgram.java invokes the compiler. Here the compiler also performs some crucial work.

  • Lexical Analysis: The compiler breaks down the source code into tokens, identifying keywords, identifiers, operators, and other language elements.
  • Syntax Analysis (Parsing): The compiler checks if the sequence of tokens adheres to the Java grammar rules. It builds a parse tree representing the structure of the code.
  • Semantic Analysis: The compiler verifies the meaning of the code, checking for type errors, variable declarations, and other semantic rules.
  • Code Generation: If the code passes all the checks, the compiler generates bytecode. Bytecode is a platform-independent set of instructions designed to be executed by the Java Virtual Machine (JVM).
java compilation from source code to bytecode

The output of the compilation process is one or more .class files. Each .class file contains the bytecode for a single class defined in the source code. In our example, compiling FirstProgram.java will produce FirstProgram.class.

JVM stands for Java Virtual Machine. It is the cornerstone of Java’s platform independence. It’s an abstract computing machine that executes Java bytecode. It takes bytecode as an input. The JVM is responsible for loading, verifying, and executing the bytecode.

When you run a Java program, the JVM’s class loader subsystem loads the necessary .class files into memory. The class loader finds the .class files based on the classpath, which is a list of directories and JAR files where the JVM searches for classes.

The class loading process involves three main phases.

  • Loading: In this stage, the classloader reads the bytecode from the .class file and creates a class object in memory.
  • Linking: This phase involves three substages:
    • Verification: Check bytecode for security and structural corrctness.
    • Preparation: The JVM allocates memory for static variables and initializes them with default values.
    • Resolution: Symbolic references in the bytecode are replaced with direct references to other classes, methods, and fields in memory.
  • Initialization: Static variables are initialized with their actual values, and the static initializer blocks are executed.
Java Class loading process

The bytecode verifier is an important component of JVM’s security model. It ensures that the bytecode is safe to execute and doesn’t contain malicious code or violate the JVM’s rules. It also performs several checks, including

  • Type Checking: Verifies that the bytecode operations are performed on the correct data types.
  • Stack Overflow/Underflow: Ensures that the operand stack is used correctly and doesn’t overflow or underflow.
  • Illegal data conversion: Checks for invalid conversions between data types.
  • Object access: Verifies that objects are accessed according to their visibility and access modifiers.
Bytecode verification process

The execution engine is the heart of the JVM, responsible for executing the bytecode. It can use different approaches to execute the bytecode:

  • Interpreter: The interpreter reads and executes each bytecode instruction one at a time. This approach is simple but relatively slow.
  • Just In Time (JIT) Compiler: Just-In-Time (JIT) compilation is a method to compile the code at runtime. It is also called dynamic translation. As a result, the code execution is sped up. A JIT compiler dynamically converts the code sections into machine code during runtime. It finds the patterns in the program execution, observes the frequently executed code sections, and optimizes them to gain efficiency. A JIT compiler maintains a balance between the compiler’s performance and the interpreter’s flexibility by using this dynamic compilation method.

The JVM also includes a garbage collector, which automatically manages memory allocation and deallocation. The garbage collector identifies the objects that are no longer needed in the program and prevents the allocation of memory for those objects.

JVM architecture overview

To run the compiled java program, you can use the java command, followed by the name of the class containing teh main method.

This command instructs the JVM to load the FirstProgram class, locate the main method, and begin execution. The JVM then executes the bytecode, which in our example will print “Hello, World!” to the console.

2 thoughts on “How Java Works: A Beginner’s Best Guide to Understanding JVM, JRE, and JDK 2025-26”

Leave a Comment