Bytecode Architecture
Understand how Java class files encode bytecode and how the JVM executes stack-based instructions.
Class File Structure
A Java class file contains the bytecode, metadata, and structure needed to define a class at runtime.
The JVM reads this file format to load, verify, and execute the class.
Magic Number
The magic number is the first field in a class file and identifies it as a valid Java class file.
It helps the JVM reject corrupted or unsupported input early.
Constant Pool
The constant pool stores symbolic constants used by the class, including string literals and class references.
It is central to bytecode linkage and constant reuse.
Access Flags
Access flags mark whether the class is public, final, abstract, interface-based, and so on.
They encode the class’s runtime visibility and semantic constraints.
Fields
Fields represent the object or class state stored inside the class definition.
They capture instance data and static members used during execution.
Methods
Methods contain bytecode, signatures, and metadata for the operations performed by the class.
The JVM resolves and invokes these methods during execution.
Attributes
Attributes store extra metadata, such as source information, debug details, annotations, and compiler-generated data.
They help the JVM and tools understand how the class should be interpreted.
Bytecode Instructions
Bytecode instructions are the operations executed by the JVM, such as load, store, arithmetic, branch, and invoke instructions.
These instructions are platform-independent and designed for virtual execution.
Stack-Based Execution
Java bytecode is stack-based rather than register-based. Instructions push operands onto the operand stack and consume them as needed.
This model makes bytecode portable and easier to verify.
Bytecode Verification
Bytecode verification checks that the class’s instructions are valid and do not violate Java safety rules.
This is a key security and correctness step before the JVM executes the code.