Title: Java Bytecode What is a .class file anyway?
1Java BytecodeWhat is a .class file anyway?
- Dan Fleck
- George Mason University
- Fall 2007
2What is in a .class file?
- Create source file
- Compile source file into .class file
- Run the .class file
So, what is in the .class file?
3The way other languages work (C, ADA, etc)
- Source code is written
- Source code is ported to every different platform
- Source code is compiled into platform specific
machine code (or binaries) - Binaries execute on a single platform
4Source Code
Linux port
Windows port
Mac port
Linux Compile
Windows Compile
Mac Compile
Linux binary
Win binary
Mac binary
Windows Machine
Linux Machine
Mac Machine
5Java Write once, compile, run anywhere!
- Java is compiled into machine independent
bytecode class files - Bytecode is interpreted by the Java Virtual
Machine (JVM) - JVM executes the step-by-step instructions given
to it from the bytecode - JVM is specific to each computer architecture
(Linux JVM, Windows JVM, Mac JVM).
6How Java Does it
Source Code
Java Compile
Linux JVM
Win JVM
Mac JVM
Windows Machine
Linux Machine
Mac Machine
7Advantages of Bytecode
- Bytecode is architecture independent (and writing
a VM is easier than rewriting a compiler for
every architecture) - VMs can enforce different levels of security
automatically (applets versus applications) - Just In-Time (JIT) compiling helps achieve same
or better speed than traditional compiled code
8Other languages that have an intermediate
representation
- C
- Perl
- PHP
- Python
- Forth
- Tcl
9Java Bytecode
- Stack-based virtual machine
- Small instruction set 202 instructions (all are
1 byte opcode operands) - Intel x86 280 instructions (1 to 17 bytes
long!) - Memory is typed
- Every Java class file begins with magic number
3405691582 0xCAFEBABE
10Referencing Memory
- iload ltvarnumgt
- Pushes (loads) the int in local variable ltvarnumgt
(1 bytes) on the stack - istore ltvarnumgt
- Pops the int on the top of the stack and stores
it in local variable ltvarnumgt - iload, fload, dload, aload (reference)
- istore, fstore, dstore, astore
11Javap examples
javap -c Test1
javac -g Test1.java
public class Test1 public int add(int a, int
b) int c ab return c
public int add(int, int) Code 0 iload_1
1 iload_2 2 iadd 3 istore_3 4
iload_3 5 ireturn
// push onto stack
// push onto stack
// add var 1 and 2
// store as local var 3
// store onto stack
// return int
Javap included with Java Development Kit (JDK)
12JAD example
jad -a Test1
javac -g Test1.java
public class Test1 public int add(int a, int
b) int c ab return c
public int add(int a, int b) int c a
b // 0 0iload_1 // 1 1iload_2 //
2 2iadd // 3 3istore_3 return c
// 4 4iload_3 // 5 5ireturn
JAD is free, but not included with Java
Development Kit (JDK)
13Java Bytecode Explanation
Opcode Mnemonic Description
0 nop Does nothing
1 aconst_null Push null on the stack
3 iconst_0 Push int 0 on the stack
4 iconst_1 Push int 1 on the stack
14Java Bytecode Explanation
Opcode Mnemonic Description
18 ldc ltvaluegt Push a one-word (4 bytes) constant onto the stack
Constant may be an int, float or String
ldc Hello ldc 201
The String is really a reference to an entry in
the string constant table!
15Java Bytecode Arithmetic
Opcode Mnemonic Description
96 iadd Pops two integers from the stack and pushes their sum
iconst_2 iconst_3 iadd
16Java Bytecode Arithmetic
Opcode Mnemonic Description
96 iadd Pops two integers from the stack and pushes their sum
97 ladd Pops two long integers from the stack and pushes their sum
106 fmul Pops two floats from the stack and pushes their product
119 dneg Pops a double from the stack, and pushes its negation
17Other types of Instructions
- Control Flow (20 instructions)
- if, goto, return
- Method Calls (4 instructions)
- Loading and Storing Variables (65 instructions)
- Creating objects (1 instruction)
- Using object fields (4 instructions)
- Arrays (3 instructions)
18Method Calls
- invokevirtual ltmethodgt
- Invokes the method ltmethodgt on the parameters and
object on the top of the stack. - Finds the appropriate method at run-time based on
the actual type of the this object.
invokevirtual ltMethod void println(java.lang.Strin
g)gt
19Control Flow
- ifeq ltlabelgt
- Pop an int off the stack. If it is zero, jump to
the label. Otherwise, continue normally. - if_icmple ltlabelgt
- Pop two ints off the stack. If the second one is
lt the first one, jump to the label. Otherwise,
continue normally.
20Static Method Calls
- invokestatic ltmethodgt
- Invokes a static (class) method ltmethodgt on the
parameters on the top of the stack. - Finds the appropriate method at run-time based on
the actual type of the this object.
21JAD example 2
javac -g Test1.java
public void count() for (int i0 ilt10 i)
System.out.println( "i is "i)
VM Spec http//java.sun.com/docs/books/jvms/secon
d_edition/html/Overview.doc.html
22JAD example
public void count() for(int i 0 i lt
10 i) // 0 0iconst_0 // 1
1istore_1 // 2 2iload_1 // 3 3bipush
10 // 4 5icmpge 39 System.out.printl
n((new StringBuilder()).append("i is
").append(i).toString()) // 5 8getstatic
2 ltField PrintStream System.outgt // 6
11new 3 ltClass StringBuildergt // 7
14dup // 8 15invokespecial 4 ltMethod
void StringBuilder()gt // 9 18ldc1
5 ltString "i is "gt // 10 20invokevirtual
6 ltMethod StringBuilder StringBuilder.append(Str
ing)gt // 11 23iload_1 // 12
24invokevirtual 7 ltMethod StringBuilder
StringBuilder.append(int)gt // 13
27invokevirtual 8 ltMethod String
StringBuilder.toString()gt // 14
30invokevirtual 9 ltMethod void
PrintStream.println(String)gt // 15 33iinc
1 1 // 16 36goto 2 // 17
39return
23JAD example 3 Test2.java
What does this method do?
// 0 0iconst_5 // 1
1istore_1 // 2 2bipush 10
// 3 4istore_2 // 4 5iload_2
// 5 6iload_1 // 6 7isub
// 7 8istore_3 // 8
9iload_3 // 9 10ireturn
24JAD example 3 Test2.java
What does this method do?
// 0 0iconst_5 // 1
1istore_1 // 2 2bipush 10
// 3 4istore_2 // 4 5iload_2
// 5 6iload_1 // 6 7isub
// 7 8istore_3 // 8
9iload_3 // 9 10ireturn
Push 5 onto stack
Store into local var 1
Push 10 onto stack
Store into local var 1
Load var 2 onto stack
Load var 1 onto stack
Subtract stack vars
Store into local var 1
Load var 3 onto stack
Return (top val from stack)
25JAD example 3 Test2.java
public int subtract() int a
5 // 0 0iconst_5 // 1
1istore_1 int b 10 // 2
2bipush 10 // 3 4istore_2
int c b - a // 4 5iload_2
// 5 6iload_1 // 6 7isub
// 7 8istore_3 return c //
8 9iload_3 // 9 10ireturn
26References
- http//ocw.mit.edu/NR/rdonlyres/Special-Programs/S
P-772Spring-2005-Summer-2005/3AFB411F-B5C0-4032-99
64-BF733EEB3199/0/bytecode.pdf - http//www.cs.virginia.edu/evans, Bytecode,
Lecture 18