You are on page 1of 37

JAVA VIRTUAL

MACHINE
(JVM)
CONTENTS
 JAVA Development tools
 What is JVM?
 JVM Architecture
 Execution Environment
 JAVA Bytecodes
 Bytecode Verifier
 Bytecode Instructions
 How does JVM work?
 JVM-a part of JRE
 Data types in JVM
JAVA DEVELOPMENT TOOLS
A virtual machine can be any platform-hardware or
software-that is capable of understanding and executing
Java bytecodes. There are currently three such platforms:
Java Virtual Machines, Just-in-Time Compilers, and Java
Processors.

Java Virtual Machine


The Java Virtual Machine is a piece of software that
translates Java bytecodes to the processor's native opcodes
as they are executed.
Just-in-Time (JIT) compilers
Interpreted languages are portable, but slow. Just-in-Time
(JIT) compilers were invented to speed the execution of Java
programs by speeding up the language translation process.
It keeps a copy of all previously translated code for
potential reuse. That way, the same section of code never
has to be reinterpreted. Only new bytecodes that have not
been previously executed need to be translated.
Java processor
Java processor is a microprocessor with a set of opcodes
that are identical to the Java bytecode standard. To
eliminate the need for native methods, these chips will
have a set of extended bytecodes for accessing memory
directly. It is reasonable to expect that these so-called
JavaChips will execute Java bytecodes as quickly as any
What Is JVM?
 The Java Virtual Machine, or JVM, is an abstract computer that
runs compiled Java programs. All Java programs are compiled
for the JVM.

 The Java Virtual Machine is a crucial component of the


Java Platform.

 JVM is a platform-independent execution environment that


converts Java bytecode into machine language and executes it.

 A JVM mimics a real Java processor, enabling Java bytecode to


be executed as actions or operating system calls on any
processor regardless of the operating system.

 A JVM is a set of computer software programs and data


structures which implements a specific virtual machine model.
The JVM is "virtual" because it is generally implemented in
software on top of a "real" hardware platform and operating
system.

 The Java Virtual Machine has a stack-based architecture


JVM Provides A Layer Of
Abstraction Between The
Compiled Java Program And The
Underlying Hardware Platform
And Operating System
COMPILED JAVA
PROGRAMS
JAVA VIRTUAL MACHINE
HARDWARE PLATFORM
AND OPERATING SYSTEM
JVM Architecture
JVM ARCHITECTURE
The behaviour of JVM implementation is defined in terms of
subsystems,datatypes and instructions. These components
form the architecture of JVM.

Every implementation of Java has a class loader system and


an execution engine.

The class loader system is responsible for loading classes and


interfaces in JVM.

The execution engine is responsible for executing the


methods of the loaded classes.

JVM needs memory to store objects, parameters to methods,


return values, or the result of any computation.

It organizes this memory in data areas such as heaps, method


areas and native method stacks.

JVM components that implement security in the execution


environment are the class loader, class file verifier,and
CLASS LOADER SUBSYSTEM

 The part of a Java virtual machine implementation


that takes care of finding and loading types is the
class loader subsystem.

 The Java virtual machine has a flexible class


loader architecture that allows a Java application
to load classes in custom ways.

 A Java virtual machine's main job is to load class


files and execute the bytecodes they contain.

 The Java virtual machine 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.
TYPES OF CLASS LOADERS

 bootstrap class loader


The bootstrap class loader (there is only one
of them) is a part of the Java virtual machine
implementation. The bootstrap class loader loads
classes, including the classes of the Java API, in
some default way, usually from the local disk.

 user-defined class loader


a Java application can install user-defined class
loaders that load classes in custom ways, such as
by downloading class files across a network .
The class loader subsystem also does:

 Loading: finding and importing the binary data for a


type
 Linking: performing verification, preparation, and
resolution
 Verification: ensuring the correctness of the imported
type
 Preparation: allocating memory for class variables and
initializing the memory to default values
 Resolution: transforming symbolic references from the
type into direct references.
 Initialization: invoking Java code that initializes class
variables to their proper starting values.

 Every Java virtual machine implementation has a


bootstrap class loader, which knows how to load
trusted classes, including the classes of the Java
API. The Java virtual machine specification doesn't
JVM needs memory to store:

 bytecodes
 parameters to methods
 return values
 local variables
 intermediate results of
computations.

The Java virtual machine organizes


this memory it needs to execute a
program into several runtime data
areas.
Run Time Data Areas
The "virtual hardware" of the Java
Virtual Machine can be divided into four
basic parts:

► The Registers

► The Stack

► The Garbage-collected Heap

► The Method Area.


•Each register in the JVM stores one 32-bit address.

•The stack and garbage-collected heap are aligned on


word (32-bit) boundaries.

•The method area, because it contains bytecodes, is


aligned on byte boundaries.

•The size of an address in the JVM is 32 bits.The JVM


can, therefore, address up to 4 gigabytes of memory,
with each memory location containing one byte.

•A word in the Java Virtual Machine is 32 bits.

•Object handle is a 32-bit address that refers to an


object on the heap.
The Method Area And The
Program Counter
 The method area is where the
bytecodes reside.
 The program counter always
points to some byte in the
method area.
 The program counter is used
to keep track of the thread of
execution.
 After a bytecode instruction
has been executed, the
program counter will contain
the address of the next
instruction to execute.
 After execution, the JVM sets
the program counter to the
address of the instruction that
immediately follows the
previous one.
The Garbage-collected Heap
 The heap is where the objects of a Java
program live.
 Any time you allocate memory with the
new operator, that memory comes from
the heap.
 The runtime environment keeps track
of the references to each object on the
heap,
 And automatically frees the memory
occupied by objects that are no longer
referenced -- a process called garbage
collection
The Java Stack And Related
Registers
 The Java stack is used
to store parameters
for and results of
bytecode instructions,
to pass parameters to
and return values
from methods, and to
keep the state of each
method invocation.

 The state of a method


invocation is called its
stack frame.

 The vars, frame, and


optop registers point RUNTIME DATA AREAS
to different parts of EXCLUSIVE TO EACH
the current stack THREAD
frame.
ere are three sections in a Java stack frame:
Contains All The
Local Variables
 Local Variables Being Used By The
Current Method
Invocation. It Is
Pointed To By The
Vars Register.
Is Used To
 Execution Environment Maintain The
Operations Of
The Stack Itself.
It Is Pointed To
By The Frame
Register.
 Operand Stack Used As A Work Space By Bytecode
Instructions.The Parameters For
Bytecode Instructions Are Placed, And
Results Of Bytecode Instructions Are
Found Here. The Top Of The Operand
Stack Is Pointed To By The Optop
EXECUTION ENGINE
At the core of any Java virtual machine implementation is its
execution engine.

In the Java virtual machine specification, the behavior of the


execution engine is defined in terms of an instruction set. For
each instruction, the specification describes in detail what an
implementation should do when it encounters the instruction
as it executes bytecodes.

The term "execution engine" can also be used in any of three


senses: an abstract specification, a concrete implementation,
or a runtime instance.

The abstract specification defines the behavior of an


execution engine in terms of the instruction set.

Concrete implementations, which may use a variety of


techniques, are either software, hardware, or a combination
of both.

A runtime instance of an execution engine is a thread. Each


EXECUTION
ENVIRONMENT
 Programs intended to run on a JVM must be
compiled into a standardized portable binary
format, which typically comes in the form of
.class files.

 Multiple class files may be packaged together


in a .jar file (short for Java archive).

 The JVM runtime executes .class or .jar files,


emulating the JVM instruction set by
interpreting it, or using a just-in-time compiler
(JIT) such as Sun's HotSpot.
JAVA BYTECODES
STEP 1 STEP 2
Java
programs are The JVM executes
compiled into
a form called Java bytecodes, so
Java Java bytecodes can be
bytecodes. thought of as the
machine language of
the JVM.

STEP 3 STEP 4

The Java compiler reads The compiler


Java language source generates one class
(.java) files, translates the file per class in the
source into Java bytecodes, source.
and places the bytecodes
into class (.class) files.
JAVA JAVA JAVA
SOURCE COMPILER BYTECODES

 To the JVM, a stream of bytecodes is a sequence of


instructions.

 Each instruction consists of a one-byte opcode and zero


or more operands.

 The opcode tells the JVM what action to take.

 If the JVM requires more information to perform the


action than just the opcode, the required information
immediately follows the opcode as operands.

 A mnemonic is defined for each bytecode instruction.

 The mnemonics can be thought of as an assembly


language for JVM.
BYTECODE VERIFIER

The JVM verifies all bytecode before it is


executed. This verification consists primarily
of three types of checks:
 Branches are always to valid locations
 Data is always initialized and references are
always type-safe
 Access to "private" or "package" data and
methods is rigidly controlled.
BYTECODE
INSTRUCTIONS
The JVM has instructions for the following
groups of tasks:
 Load and store
 Arithmetic
 Type conversion
 Object creation and manipulation
 Operand stack management (push / pop)
 Control transfer (branching)
 Method invocation and return
 Throwing exceptions
 Monitor-based concurrency
How Does JVM Work?
 Most programming languages compile source
code directly into machine code, suitable for
execution on a particular microprocessor
architecture.

 The difference with Java is that it uses


bytecode - a special type of machine code. 

 Java bytecode executes on a special type of


microprocessor. There wasn't a hardware
implementation of this microprocessor
available when Java was first released.

 Instead, the processor architecture is


emulated by what is known as a "virtual
machine".
JVM emulation run on a
 This virtual machine is an emulation of a real physical CPU
Java processor - a machine within a machine .

 The only difference is that the virtual machine


isn't running on a CPU - it is being emulated on
the CPU of the host machine.
JVM-a part of JRE
 The Java Virtual Machine forms part of a
large system, the Java Runtime
Environment (JRE).

 The JRE comprises a set of base classes,


which are an implementation of the
base Java API, as well as a JVM.

 Without an available JRE for a given


environment, it is impossible to run Java
software.
The JVM is responsible for interpreting Java bytecode,
and translating this into actions or operating system
calls.
JVM “ Lean And Mean ! “

The JVM is LEAN because it


is small when implemented in
software. It was designed to be
small so that it can fit in as
many places as possible --
places like TV sets, cell The JVM is MEAN
phones, and personal because it of its ambition. It
computers. wants to be everywhere, and
its success is indicated by
the extent to which
programs written in Java
will run everywhere.
DATA TYPES IN JVM
The JVM has a small number of primitive data
types:
The integral types are:
► byte ( 8 bits)
► short (16 bits)
► int (32 bits)
► Long (64 bits)
► char (16 bits).
The floating types are:
► float (32 bits)
► double (64 bits)
 The data types can be divided into a set of
primitive types and a reference type.

 Variables of the primitive types hold


primitive values, and variables of the
reference type hold reference values.

 Reference values refer to objects, but are


not objects themselves.

 Primitive values, by contrast, do not refer


to anything.
PRIMITIVE TYPES
 All the primitive types of the Java programming language are
primitive types of the Java virtual machine.

 Boolean qualifies as a primitive type of the JVM. When a


compiler translates Java source code into bytecodes, it uses
ints or bytes to represent booleans.

 In the JVM, false is represented by integer zero and true by


any non-zero integer.

 Operations involving boolean values use ints. Arrays of boolean


are accessed as arrays of byte.

 The primitive types of the Java programming language other


than boolean form the numeric types of the Java virtual
machine.

 The JVM works with one other primitive type that is unavailable
to the Java programmer: the returnAddress type. It is used to
implement finally clauses of Java programs.
REFERENCE TYPE
 Values of type reference come in three flavors:
 class type- references to class instances
 interface type- references to class instances that
implement an interface
 array type- references to arrays, which are full-fledged
objects in JVM.

 All three types have values that are references to


dynamically created objects.

 One other reference value is the null value, which


indicates the reference variable doesn't refer to any
object.
DATA TYPES OF THE JAVA VIRTUAL
MACHINE
Type Range

byte 8-bit signed two's complement integer (-27 to 27 - 1, inclusive)

short 16-bit signed two's complement integer (-215 to 215 - 1, inclusive)

int 32-bit signed two's complement integer (-231 to 231 - 1, inclusive)

long 64-bit signed two's complement integer (-263 to 263 - 1, inclusive)

char 16-bit unsigned unicode character (0 to 216 - 1, inclusive)

float 32-bit IEEE 754 single-precision float

double 64-bit IEEE 754 double-precision float


Summary
The Java Virtual Machine provides a
platform-independent way of executing
code, by abstracting the differences
between operating systems and CPU
architectures. Java Runtime Environments
are available for a wide variety of
hardware and software combinations,
making Java a very portable language.
Programmers can concentrate on writing
software, without having to be concerned
with how or where it will run. The idea of
virtual machines is nothing new, but Java
is the most widely used virtual machine
used today. Thanks to the JVM, the dream

You might also like