Bytecode
Bytecode is the compact instruction set that javac produces and the JVM executes. It is stored in .class files and is portable across every machine that has a JVM.
Detailed Explanation
When you compile Order.java you get Order.class. That file is not machine code for your laptop. It is a sequence of one-byte opcodes for an imaginary processor, the JVM, plus the metadata the JVM needs: the constant pool, field and method descriptors, and attributes.
The reason bytecode exists is that it splits one hard problem into two easy ones. javac handles the language: parsing, type checking, resolving overloads, desugaring newer syntax. The JVM handles the machine: register allocation, instruction selection, cache behaviour. Neither side needs to know about the other, which is why a new Java version can add pattern matching without any CPU vendor being involved.
The JVM is a stack machine, not a register machine. Nearly every instruction pushes operands onto an operand stack or pops them off. iload_1 pushes local variable 1 as an int. iadd pops two ints and pushes their sum. invokevirtual pops the receiver and arguments and calls a method. This design keeps instructions short and makes verification tractable, at the cost of being slower to interpret than register code. The JIT compiler erases that cost by translating hot methods to real register-based machine code.
Bytecode is typed, and the types are coarser than Java's. There are opcodes for int, long, float, double, and reference. There are none for boolean, byte, short, or char arithmetic, so those are computed as int and narrowed on store. Generics are gone entirely: List<String> and List<Integer> compile to the same instructions, which is type erasure.
Several language features are pure compiler tricks that leave no trace. String concatenation with + becomes a call through invokedynamic since Java 9. Enhanced for over a collection becomes an Iterator loop. Lambdas become an invokedynamic call site that spins up an implementation class at runtime rather than a hidden anonymous class. Knowing this stops you from guessing about performance: you can look.
javap -c is the tool. Reading the disassembly answers questions that source code cannot: whether a concatenation in a loop allocates, whether a final field got constant-folded, whether a synthetic bridge method was generated for a covariant return.
You do not write bytecode by hand. You read it for three reasons: to explain a performance surprise, to understand a decompiled third-party library, or to work with tools that generate it. Spring, Hibernate, Mockito, and every code coverage agent manipulate bytecode at load time using ASM or ByteBuddy. That is how a proxy can intercept a method call you never wrote an interface for.
The costs are mostly indirect. Bytecode is compact but must be verified and then interpreted before it gets compiled, which is why JVM startup lags a native binary. It is also easy to decompile, so shipping a JAR is close to shipping source unless you obfuscate. The benefit is that one artifact runs everywhere and the runtime can optimise it with information a static compiler never sees.
Code Examples
public class Sum {
// Compile, then run: javap -c Sum
static int add(int a, int b) {
return a + b;
}
// Disassembles to roughly:
// 0: iload_0 // push parameter a
// 1: iload_1 // push parameter b
// 2: iadd // pop both, push the sum
// 3: ireturn // return the int on top of the stack
}import java.util.List;
public class Erasure {
// Both methods have the same bytecode signature after erasure,
// so this class does NOT compile: "name clash, same erasure".
// void handle(List<String> ids) { }
// void handle(List<Integer> ids) { }
// Erasure is also why this prints true.
public static void main(String[] args) {
List<String> names = List.of("ada");
List<Integer> ports = List.of(8080);
System.out.println(names.getClass() == ports.getClass());
// prints: true — at runtime both are just ImmutableCollections.List12
}
}Common Gotchas
The class file version is a hard gate
each Java release bumps the major version stored in every class file (Java 17 is 61, Java 21 is 65). An older JVM refuses newer class files with `UnsupportedClassVersionError` rather than trying and failing.
Bytecode compatibility and source compatibility are different
recompiling against a changed library is not the same as dropping in a new JAR. Changing a `static final` constant's value, for example, does not take effect in already-compiled callers, because the compiler inlined the old value into their bytecode.
Optimisation you read in bytecode may not be the whole story
`javac` does almost no optimisation. The interesting work happens in the JIT at runtime, so unimpressive-looking bytecode can still run fast.
Anyone can read your shipped code
a `.class` file decompiles back to close-to-original Java. Never put credentials, API keys, or licence logic you care about into a distributed JAR.