SyntaxStudy
Sign Up
Java Understanding the Java Compilation Model and the JVM
Java Beginner 1 min read

Understanding the Java Compilation Model and the JVM

When you compile a Java source file with javac, the compiler translates it into platform-neutral bytecode stored in .class files. Bytecode is not machine code; it is an intermediate representation that the JVM can interpret or just-in-time (JIT) compile into native instructions at runtime. This two-step process is central to Java portability: the same .class file runs on Windows, Linux, or macOS as long as a JVM is present. The JVM is more than a simple interpreter. Modern JVMs such as HotSpot use JIT compilation, adaptive optimization, and garbage collection to achieve performance close to native code. The JVM also enforces type safety and memory safety at runtime, catching errors like null pointer dereferences and array index out of bounds before they corrupt memory. Java programs are organized into packages, which map to directory structures on disk. The javac command accepts a source path and a class path so it can resolve references across multiple files. The java command similarly needs the class path to locate the compiled classes when starting the JVM.
Example
// File: JvmDemo.java
// Demonstrates compilation units, packages, and basic JVM information

package com.example.intro;  // maps to directory: com/example/intro/

public class JvmDemo {

    public static void main(String[] args) {

        // ---- Runtime / JVM info ----
        Runtime rt = Runtime.getRuntime();

        long maxMemory  = rt.maxMemory()  / (1024 * 1024); // convert to MB
        long totalMemory = rt.totalMemory() / (1024 * 1024);
        long freeMemory  = rt.freeMemory()  / (1024 * 1024);
        int  processors  = rt.availableProcessors();

        System.out.println("=== JVM Runtime Information ===");
        System.out.printf("Max heap memory  : %d MB%n", maxMemory);
        System.out.printf("Total heap memory: %d MB%n", totalMemory);
        System.out.printf("Free heap memory : %d MB%n", freeMemory);
        System.out.printf("Available CPUs   : %d%n",   processors);

        // ---- System properties ----
        System.out.println("\n=== Key System Properties ===");
        String[] keys = {"java.version", "java.vendor", "os.name", "user.dir"};
        for (String key : keys) {
            System.out.printf("%-20s = %s%n", key, System.getProperty(key));
        }
    }
}