Skip to content

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 .class files, 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, invokevirtual and return; javap -c Hello prints 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.

The JVM instruction set (JVMS Chapter 6): one-byte opcodes such as new (0xbb), dup (0x59) and invokespecial (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 .

Bytecode in the glossary

Class file

JVM specification

The 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 #13 means “use entry 13”

The label on the box

The magic number CAFEBABE and 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.

Header and constant pool excerpt (javap -v Main.class)Output
  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

JVM Atlas

Explore the machine behind Java programs. A free, open-source atlas of the Java Virtual Machine. Specification baseline: Java SE 25. Implementation details: OpenJDK HotSpot, JDK 25.

MIT licensed · no accounts, no tracking, no backend · your progress stays in your browser.

JVM Atlas is an independent educational project and is not affiliated with or endorsed by Oracle. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.