Understanding the Java platform (JVM, bytecode)
Understanding the Java platform (JVM, bytecode)
Section titled “Understanding the Java platform (JVM, bytecode)”The Java platform is a software‑only environment that runs Java applications. It consists of two main parts:
Java Virtual Machine (JVM)
Section titled “Java Virtual Machine (JVM)”The JVM is the heart of the platform. It is an abstract computing machine that provides:
- Bytecode execution – interprets and/or compiles bytecode to native code.
- Memory management – automatic garbage collection.
- Security – bytecode verification, sandboxing.
- Platform abstraction – isolates the program from the underlying OS.
Different JVMs exist for Windows, Linux, macOS, etc., but they all understand the same bytecode format.
Bytecode
Section titled “Bytecode”Java source code is compiled to bytecode – a set of instructions designed to be executed by the JVM. Bytecode is platform‑independent, which enables “Write Once, Run Anywhere”.
How a Java program runs:
Section titled “How a Java program runs:”- Compilation:
javacconverts.java→.class(bytecode). - Class loading: The JVM’s class loader loads the
.classfile into memory. - Bytecode verification: The verifier checks bytecode for illegal operations.
- Execution: The interpreter executes bytecode. For frequently executed code (“hot spots”), the Just‑In‑Time (JIT) compiler compiles the bytecode to native machine code for performance.
Bytecode example
Section titled “Bytecode example”Given this code:
public class Demo { public static void main(String[] args) { int a = 5; int b = 10; System.out.println(a + b); }}After compilation, you can view the bytecode with javap -c Demo:
0: iconst_51: istore_12: bipush 103: istore_24: getstatic #2 // Field java/lang/System.out5: iload_16: iload_27: iadd8: invokevirtual #3 // Method println(I)V9: returnThis shows the low‑level operations performed by the JVM.
JVM implementations
Section titled “JVM implementations”- HotSpot (Oracle’s default)
- OpenJ9 (Eclipse’s efficient JVM)
- GraalVM (high‑performance, polyglot JVM)
All are compatible with the Java bytecode standard.