You are on page 1of 17

JAVA VIRTUAL

MACHINE
(JVM)
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
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".

 This virtual machine is an emulation of a real Java


processor - a machine within a machine . JVM emulation run on a
physical CPU
 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.
The JVM is responsible for interpreting Java bytecode, and translating
this into actions or operating system calls.
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.
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
computers. ambition. It wants to be
everywhere, and its
success is indicated by
the extent to which
programs written in Java
will run everywhere.
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.


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 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 to different parts of the current stack frame.

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
•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.


DATA TYPES IN JVM

The JVM has a small number of primitive data


types:
► byte ( 8 bits)
► short (16 bits)
► int (32 bits)
► Long (64 bits)
► float (32 bits)
► double (64 bits)

► char (16 bits).


There are three sections in a Java stack frame:
Contains All The Local
Variables Being Used By
 Local Variables The Current Method
Invocation. It Is Pointed
To By The Vars Register.

Is Used To Maintain
 Execution Environment 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
Register.

You might also like