Notes
Outline
Java Virtual Machine (JVM)
Reasons for learning JVM
Resource
The Java TM Virtual Machine Specification (2 nd Ed), by Tim Lindholm & Frank Yellin, Addison-Wesley,1999
http://java.sun.com/docs/books/vmspec/
JVM Types and Words
byte, char, short, int, float, reference, and returnAddress in one word
boolean, byte, char, and short are converted to int
Special representations for byte, char, and short arrays are possible
long and double in two words
JVM Architecture
Data Areas of JVM
Method area (shared by all threads)
One record for each loaded class that contains:
Constant pool
Code for methods and constructors
Heap (shared by all threads)
One record for each class instance or array
Stack (one per thread)
Hold frames associated with method invocations
Stack Frame Structure
The class File Format
Names and Descriptors
Class, method, and field names are all stored symbolically as strings
A descriptor is a string representing the type of a field or method
Class names
Class names are always fully qualified
Use forward slash rather than dot as the delimeter
Ex: Java.util.Vector =>
java/util/Vector
Descriptors
Primitive types
B, C, D, F, I,J(long), S, Z(boolean), V(void)
Arrays
Use [
Classes
Lclassname
Ex:
Type: String valueOf(char[] , int offset, int count)
Descriptor:([CII)Ljava/lang/String;
Constants
CONSTANT_Class
CONSTANT_Fieldref
CONSTANT_Methodref
CONSTANT_InterfaceMthodref
CONSTANT_String,
CONSTANT_Integer
CONSTANT_Float
CONSTANT_Long
CONSTANT_Double
CONSTANT_NameAndType
CONSTANT_Utf8
field_info and method_info
field_info
Access flags
Name
Descriptor
Static values
method_info
Access flags
Name
Descriptor
Code
Instruction Set
Load and store (e.g., iload, istore ldc, iconst_<i>)
Arithmetic (e.g., iadd, isub, imul, idiv, irem)
Type conversion (e.g., i2b, i2f, i2d)
Object creation and manipulation
(e.g, new, newarray, iaload, iastore , getfield,  putfield)
Operand stack manipulation (e.g., pop, dup, dup_x1, swap)
Control transfer (e.g., goto, ifeq, tableswitch)
Method invocation and return
(invokevirtual, invokeinterface, invokespecial, invokestatic, ireturn)
Exception handling and synchronization (e.g.,athrow)
Compiling for JVM
Constants, Local Variables, and Control constructs
Compiling for JVM
Receiving Arguments and Invoking Methods
Compiling for JVM
Working with Class Instances
Compiling for JVM
Arrays
Compiling for JVM
Switches
Compiling for JVM
Manipulation of the Operand Stack
Compiling for JVM
Exception Handling
Review Questions
Does the efficiency of a program change after an int variable is changed to byte?
When does dynamic loading take place?
When does dynamic linking take place?
Why are run-time byte-code verification and type checking necessary?
How are exceptions handled in JVM?
How different is the JVM from a stack machine for Pascal?