Bytecode Basics
What is Java Bytecode?
Java source code (.java files) is compiled into Java bytecode (contained in .class files). This bytecode is a platform-independent intermediate language that the Java Virtual Machine (JVM) understands.
Source Code vs Bytecode
Java Source Code:
public class Example {
public void greet(String name) {
System.out.println("Hello, " + name);
}
}
Compiled Bytecode (Conceptual):
aload_0
aload_1
invokedynamic <concat>
getstatic System.out
swap
invokevirtual println
return
The bytecode version is more verbose and operating-system independent.
Why is Bytecode Important?
- Platform Independence: Works on any system with a JVM
- Runtime Flexibility: Can be transformed before loading
- Security: JVM can verify bytecode correctness
- Optimization: JVM can JIT-compile to native code
- Introspection: Tools can analyze bytecode without source code
Reading Bytecode
Tools for Inspection
- javap: Built-in Java disassembler
- Bytecode Viewer: GUI tool for inspecting bytecode
- ASM Tree View: Plugin for IDE visualization
Example: Using javap
javap -c Example.class
Output shows the bytecode instructions for each method.
Common Bytecode Instructions
Some frequently encountered bytecode instructions:
| Instruction | Purpose |
|---|---|
aload | Load object reference |
iload | Load integer |
invoke* | Call methods (static, virtual, etc) |
return | Return from method |
getstatic | Read static field |
putstatic | Write static field |
new | Create new object |
Method Descriptors (Signatures)
Bytecode uses a special notation for method signatures:
(ParameterTypes)ReturnType
Primitive Types
Z- booleanB- byteC- charS- shortI- intJ- longF- floatD- doubleV- void
Object Types
Ljava/lang/String;- String classL...classname...;- Any class
Array Types
[I- int array[Ljava/lang/String;- String array
Examples
(II)I- Adds two integers:int method(int a, int b) { return ...; }(Ljava/lang/String;)V- Takes string, returns nothing:void method(String s) { ... }()Ljava/lang/String;- No parameters, returns string:String method() { ... }([Ljava/lang/String;)V- Takes string array:void method(String[] args) { ... }
Class References
Classes are referenced using their fully qualified names with / separators:
java/lang/Stringjava/util/ArrayListcom/mycompany/MyClass
In bytekin, we typically use the standard Java notation with dots:
java.lang.Stringjava.util.ArrayListcom.mycompany.MyClass
The Class File Format
A compiled .class file contains:
- Magic Number: Identifies it as a class file (
0xCAFEBABE) - Version: Java version information
- Constant Pool: Strings, method names, field names, type information
- Access Flags: public, final, abstract, etc.
- This Class: Class name
- Super Class: Parent class
- Interfaces: Implemented interfaces
- Fields: Class member variables
- Methods: Methods with their bytecode
- Attributes: Additional metadata
Important Notes
- Bytecode is not human-readable but is systematic and analyzable
- Every Java source construct maps to bytecode
- Bytecode is verifiable - JVM checks correctness before execution
- Bytecode can be manipulated programmatically without source code
Next Steps
- Learn how bytekin Works
- Understand Core Concepts