Java learning pathSection 17

Bytecode Architecture

Understand how Java class files encode bytecode and how the JVM executes stack-based instructions.

1

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.

Java source passes through javac to a class file containing a constant pool and bytecode instructions; verified instructions execute in the JVM using an operand stack.
Class File Structure
2

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.

3

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.

4

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.

5

Fields

Fields represent the object or class state stored inside the class definition.

They capture instance data and static members used during execution.

6

Methods

Methods contain bytecode, signatures, and metadata for the operations performed by the class.

The JVM resolves and invokes these methods during execution.

7

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.

8

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.

9

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.

10

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.