Classfile format and bytecode
What you will learn
The JVM executes instructions in a class file. javac translates Java source into bytecode; the runtime may interpret it or compile it to native instructions. Inspecting a tiny trusted class makes those boundaries concrete.
- Trace a method descriptor and operand stack.
- Distinguish source, bytecode and native code.
- Identify classfile version and runtime areas.
Each example is a complete Main.java file. Use a fresh lab-owned directory: javac --release 21 -proc:none Main.java, then java Main. Use only trusted, locally compiled classes and fixed synthetic inputs. The runner enforces a process deadline.
Add two parameters
public class Main { static int add(int a, int b) { return a + b; } public static void main(String[] args) throws Exception { System.out.println(add(7, 5)); }}Expected output:
12Inspect add with javap to see the two local loads, addition and return.
Classfile structure
The first eight bytes contain the CAFEBABE magic number followed by minor and major versions. A constant pool stores symbolic names, literals and other entries; fields, methods and attributes describe the type. The Code attribute contains instructions and frame size information for ordinary concrete Java methods. Java 21 emits major version 65; Java 25 emits 69 by default. Compile with --release for the required platform; a supported classfile version alone does not ensure all dependencies are available.
Trace the operand stack
A static add(int a, int b) has descriptor (II)I: two ints in, one int out. A typical javac sequence is iload_0, iload_1, iadd, ireturn. Initially the parameters occupy local slots 0 and 1. The operand stack progresses [] → [a] → [a,b] → [a+b]; ireturn transfers the result to the caller. Run javap -c -s -v Main after compilation to inspect actual instructions, descriptors and classfile information. This is not a native disassembly or a promised compiler instruction sequence.
Runtime data areas
Each thread has its own JVM stack and execution position (the pc register is undefined during a native method). Each invocation conceptually has a frame with local variables, operand stack and linkage information. The heap is shared and conceptually stores instances and arrays. The shared method area contains per-class structures such as runtime constant pools. HotSpot metaspace is native memory used for much class metadata, not an exact synonym for the specification method area. The specification does not mandate physical layouts; optimization may eliminate allocations or frames.
Read your own classfile header
public class Main {
public static void main(String[] args) throws Exception { try (var raw = Main.class.getResourceAsStream("Main.class")) { if (raw == null) throw new IllegalStateException("missing class"); try (var in = new java.io.DataInputStream(raw)) { System.out.printf(java.util.Locale.ROOT, "%08X%n", in.readInt()); System.out.println(in.readUnsignedShort()); System.out.println(in.readUnsignedShort()); } } }}Expected output:
CAFEBABE065This reads the header of the trusted class compiled with --release 21, without preview features. It is not a classfile validator.
Follow nested calls
public class Main { static int twice(int n) { return n * 2; } static int total(int n) { return twice(n) + 2; } public static void main(String[] args) throws Exception { System.out.println(total(4)); }}Expected output:
10total calls twice and adds two. Each call conceptually has a frame, although inlining may remove physical call overhead.
Common mistakes
Do not equate javap output with machine code, bytecode length with speed, local slots with fixed memory addresses or classfile version with complete API compatibility.
Practice
Implement static int cost(int units, int price) returning their product; print cost(3, 7). Predict the descriptor and stack changes before inspecting javap.
Expected output:
21Compare your answer with the manual practice criteria.
Official reading: VM structure · Classfile · javap. Java 25 execution remains unverified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Classfile format and bytecode — knowledge check
Original JVM architecture questions.
What does javac ordinarily produce?
What does (II)I describe?
What does iadd do?
Which area is shared by threads?
What does javap -c show?