Video in progress
What is bytecode, and what is actually inside a .class file?
Bytecode is the set of platform-independent intermediate instructions that javac produces. A .class file holds those instructions, the constant pool, fields, methods and other metadata.
📖 Answer
The command javac Hello.java produces bytecode, not machine code. Every instruction starts with a one-byte opcode (hence the name): iload, iadd, invokevirtual, new, areturn and so on.
The structure of a .class file in brief:
- the magic number
0xCAFEBABE: it says the file really is a class file; - the version (major
65for Java 21); - the constant pool: strings, class and method names, numeric constants;
- access flags, the superclass, interfaces;
- fields and methods; inside the methods, the
Codeattribute, which is the bytecode itself.
Bytecode is stack-based: operands are pushed onto the operand stack, and an instruction takes them and pushes the result back. For example a + b becomes roughly iload_1, iload_2, iadd.
If you want to look inside: javap -c Hello.class.
Sign up to mark studied questions and save your progress.