Guide
What is Java bytecode?
What javac produces, how a class file stores bytecode and its constant pool, and how the JVM runs it, explained simply and in depth with a verified javap listing.
Bytecode
- Bytecode
The compact instruction language inside
.classfiles, designed for the JVM rather than for any particular CPU.Each method in a class file is a list of bytecode instructions such as
getstatic,ldc,iadd,invokevirtualandreturn;javap -c Helloprints them. Bytecode works on a small stack per method call (the operand stack) rather than on processor registers, which is one reason the same class file runs on every platform.Bytecode in the glossaryThe JVM instruction set (JVMS Chapter 6): one-byte opcodes such as
new(0xbb),dup(0x59) andinvokespecial(0xb7), followed by operands that are often indexes into the . Bytecode is a stack-machine code: instructions take operands from, and push results onto, the .
Class file
JVM specificationThe binary format the JVM loads: constant pool, fields, methods and bytecode.
Simply put
A class file is the compiled form of one class. It contains the class name, its fields and methods, the instructions for each method, and a table of names and constants (the ) that the instructions refer to.
What a developer sees
javac writes one .class file per class, including nested and anonymous classes (Outer$Inner.class, Outer$1.class). javap -c -v Hello prints what is inside: the version, the constant pool and each method’s bytecode.
Class files are what you ship, usually zipped into a JAR. A JVM refuses a class compiled for a newer Java version with UnsupportedClassVersionError.
Picture it
A flat-pack furniture kit: a numbered parts list and a sheet of step-by-step instructions that refer to the parts by number.
- The instruction sheet
The bytecode of each method
- The numbered parts list
The constant pool:
ldc #13means “use entry 13”- The label on the box
The magic number
CAFEBABEand the class-file version
Where the picture stops working
Furniture is assembled once. The JVM runs a method’s instructions every time the method is called, and may translate busy ones into machine code. A class file also contains no objects: they are created only when the program runs.
Deep dive: the specification and HotSpot in detail
Defined by JVMS §4.1: magic (0xCAFEBABE), minor_version/major_version (69 for Java SE 25), the constant_pool, access flags, this_class, super_class, interfaces, fields, methods and attributes. Each method's Code attribute holds max_stack, max_locals, the bytecode, the exception table and a StackMapTable used by the verifier. Instructions never embed names or addresses: they hold indexes into the constant pool, which keeps symbolic references until run time.
minor version: 0
major version: 69
flags: (0x0021) ACC_PUBLIC, ACC_SUPER
Constant pool:
#7 = Class #8 // Person
#9 = String #10 // Alex
#11 = Methodref #7.#12 // Person."<init>":(Ljava/lang/String;)V
#14 = Fieldref #15.#16 // java/lang/System.out:Ljava/io/PrintStream;
#20 = Methodref #7.#21 // Person.getName:()Ljava/lang/String;
Verified with OpenJDK 25.0.4 · Linux x64
Common misconceptions
A class file contains machine code.
It contains JVM bytecode, which is independent of the CPU. Machine code is produced later, inside the running JVM, by the interpreter's templates or the JIT compiler.
Sources
- SpecJVMS §4.1 The ClassFile Structure(opens in a new tab)
Magic number, versions, constant pool, fields, methods and attributes. · Java SE 25
- SpecJVMS §4.4 The Constant Pool(opens in a new tab)
Constant pool entry kinds such as Class, Fieldref, Methodref and String. · Java SE 25
- ToolThe javap command(opens in a new tab)
Disassembles class files: constant pool, descriptors and bytecode. · JDK 25