Features of Java
• Object-Oriented
• Robust
• Architecture Neutral and Platform Independent
• Multithreaded
• Dynamic
• Distributed
Java's Architecture
Java's architecture arises out of four distinct but
interrelated technologies:
•Java programming language
•Java class file format
•Java Application Programming Interface
•Java virtual machine
Relationship between these four parts
JVM and Java API form a "platform" for which all Java programs are
compiled.
The Java Virtual Machine
The Java virtual machine is an abstract computer
Java Runtime contains a class loader, which loads class files
from both the program and the Java API.
Only those class files from the Java API that are actually needed
by a running program are loaded into the virtual machine.
The bytecodes are executed in an execution engine.
On a Java virtual machine implemented in software, the simplest
kind of execution engine just interprets the bytecodes one at a
time.
Another kind of execution engine, one that is faster but requires
more memory, is a just-in-time compiler. In this scheme, the
bytecodes of a method are compiled to native machine code
the first time the method is invoked.
The native machine code for the method is then cached, so it can
be re-used the next time that same method is invoked.
Java Virtual Machine Architecture
Each instance of the Java virtual machine has one method area
and one heap.
shared by
all threads
running
inside JVM
When the virtual machine loads a class file, It places this type
information into the method area.
As the program runs, the virtual machine places all objects the
program instantiates onto the heap
As each new thread comes into existence, it gets its own
pc register (program counter) and Java stack.
If the thread is executing a Java method, the value of the pc
register indicates the next instruction to execute.
The state of native method invocations is stored in an
implementation-dependent way in native method stacks.
The Java stack is composed of stack frames.
A stack frame contains the state of one Java method invocation.
When a thread invokes a method, the JVM pushes a new frame
onto that thread's Java stack.
When the method completes, the JVM pops and discards the
frame for that method.
The Stack Frame
The stack frame has three parts:
local variables, operand stack, and frame data.
The sizes of the local variables and operand stack, which are
measured in words, depend upon the needs of each individual
method
When the JVM invokes a Java method, it checks the class
data to determine the number of words required by the
method in the local variables and operand stack.
It creates a stack frame of the proper size for the method and
pushes it onto the Java stack.
StackFrame : Local Variables
All values in the local variables are word-aligned
Values of type int, float and reference occupy one entry in the
local variables array.
Values of type byte, short, and char are converted to int before
being stored into the local variables.
Values of type long and double occupy two consecutive
entries in the array.
Method parameters on the local variables of a Java stack.
class Ex {
public static int runClassMethod(int i, long l, float f, double d,
Object o, byte b) { return 0; }
public int runInstanceMethod(char c, double d, short s, boolean b)
{ return 0; }
}
StackFrame: Operand Stack
Like the local variables, the operand stack is organized as an
array of words.
But unlike the local variables, which are accessed via array
indices, the operand stack is accessed by pushing and popping
values.
It converts values of type byte, short, and char to int before
pushing them onto the operand stack.
Here is how a Java virtual machine would add two local variables
that contain ints and store the int result in a third local variable:
iload_0 // push the int in local variable 0
iload_1 // push the int in local variable 1
iadd // pop two ints, add them, push result
istore_2 // pop int, store into local variable 2
Implementations of the Java Stack
public static void addAndPrint() {
double result = addTwoTypes(1, 88.88);
}
public static double addTwoTypes(int i, double d)
{ return i + d; }
The Class Loader Subsystem
Java virtual machine contains two kinds of class loaders:
Bootstrap Class loader and User-Defined Class loaders.
The bootstrap class loader is a part of the JVM implementation
User-defined class loaders are part of the running Java
application.
Classes loaded by different class loaders are placed into separate
name spaces inside the Java virtual machine