3 - JVM As An Interpreter and Emulator

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 34

Subject Name Java Programming (CSE III Year)

Department of Computer Science & Engineering


Created By: Dr. Avinash Dwivedi

JIMS Engineering Management Technical Campus

Greater Noida, UP - 201303


(Affiliated to Guru Gobind Singh Indraprastha University, New Delhi)
Recognized u/s 2(f) by UGC & Accredited with ‘A’ Grade by NAAC
Participant of UNGC & UNPRME, New York
ISO 9001:2015 Quality Certified
Subject: Java Programming
Topic: JVM as an interpreter and emulator
List of Topics to be covered

▰ Java Development Kit (JDK)


▰ Java Runtime Environment (JRE)
▰ Java Virtual Machine (JVM)
▰ Differences between JDK, JRE and JVM
▰ JRE and JDK work
▰ JVM Architecture
▰ Interactions between JDK and JRE
▰ Interpreter & Emulator
JAVA DEVELOPMENT KIT

▰ The Java Development Kit (JDK) is a software development


environment used for developing Java applications and applets.
▰ It includes
▰ Java Runtime Environment (JRE),
▰ an interpreter/loader (Java),
▰ a compiler (javac),
▰ an archiver (jar),
▰ a documentation generator (Javadoc)
▰ Syntax C:>javadoc filename
▰ and other tools needed in Java development.
JAVA RUNTIME ENVIRONMENT(JRE)

▰ It consists of the Java Virtual Machine (JVM), core classes,


and supporting files.
▰ a runtime environment is a piece of software that is designed to run other
software. As the runtime environment for Java, the JRE contains the Java class
libraries, the Java class loader, and the Java Virtual Machine.

▰ In this system:
▰ The class loader is responsible for correctly loading classes and connecting
them with the core Java class libraries.
▰ The JVM is responsible for ensuring Java applications have the resources they
need to run and perform well in your device or cloud environment.
▰ The JRE is mainly a container for those other components, and is
responsible for orchestrating their activities.
JAVA VIRTUAL MACHINE (JVM)

▰ JVM (Java Virtual Machine) is an abstract machine. It is called a virtual


machine because it doesn't physically exist.

▰ It is a specification that provides a runtime environment in which Java


bytecode can be executed.
It can also run those programs which are written in other languages and
compiled to Java bytecode.

▰ Runtime Instance Whenever you write java command on the command


prompt to run the java class, an instance of JVM is created.
▰ Ex C:\Raj>java simple
JRE
Differences between JDK, JRE and JVM
Differences between JDK, JRE and JVM

▰ JDK – Java Development Kit (in short JDK) is Kit which provides the environment
to develop and execute(run) the Java program. JDK is a kit(or package) which includes
two things
▻ Development Tools(to provide an environment to develop your java programs)

▻ JRE (to execute your java program).


▰ JRE – Java Runtime Environment (to say JRE) is an installation package which
provides environment to only run(not develop) the java program(or application)onto your
machine. JRE is only used by them who only wants to run the Java Programs i.e. end users
of your system.
▰ JVM – Java Virtual machine(JVM) is a very important part of both JDK and JRE
because it is contained or inbuilt in both. Whatever
▰ Java program you run using JRE or JDK goes into JVM and JVM is responsible
for executing the java program line by line hence it is also known as interpreter.
JRE and JDK work
What does JRE consists of?

▰ JRE consists of the following components:


▰ Deployment technologies, including deployment, Java Web Start and Java
Plug-in.
▰ User interface toolkits, including Abstract Window Toolkit (AWT), Swing, Java
2D, Accessibility, Image I/O, Print Service, Sound, drag and drop
(DnD) and input methods.
▰ Integration libraries, including 
Interface Definition Language (IDL),
Java Database Connectivity (JDBC),
Java Naming and Directory Interface (JNDI),
Remote Method Invocation (RMI),
Remote Method Invocation Over Internet
Inter-Orb Protocol (RMI-IIOP) and scripting.
What does JRE consists of? (Contd…)


Other base libraries, including international support,
input/output (I/O), extension mechanism, Beans, Java
Management Extensions (JMX), Java Native Interface (JNI),
Math, Networking, Override Mechanism, Security,
Serialization and Java for XML Processing (XML JAXP).

▰ Lang and util base libraries, including lang and util,


management, versioning, zip, instrument, reflection, Collections,
Concurrency Utilities, Java Archive (JAR), Logging, Preferences
API, Ref Objects and Regular Expressions.
▰ Java Virtual Machine (JVM), including Java HotSpot
Client and Server Virtual Machines.
Different JVMS

JVMs are available for many hardware and software platforms. JVM,
JRE, and JDK are platform dependent because the configuration of
each OS is different from each other. However, Java is platform
independent.

Bytecode

Java Java Java


Interpreter Interpreter Interpreter
...
on Windows on Linux on Sun Solaris
JVM Work

▰ There are three notions of the


JVM: specification, implementation, and instance.

▰ The JVM performs the following main tasks:


▰ Loads code
▰ Verifies code
▰ Executes code
▰ Provides runtime environment
JVM Architecture
JVM Architecture
ClassLoader Subsystem

▰ The classloader subsystem is an essential core of the Java Virtual machine


and is used for loading/reading the .class files and saving the bytecode in
the JVM method area.
▰ This subsystem handles the dynamic class loading functionality and
performs three major functions i.e.:

▰ Loading: This component handles the loading of the .class files from the
hardware system into the JVM memory and stores the binary data (such as
fully qualified class-name, immediate parent class-name, information
about methods, variables, constructors etc.) in the method areas.
▰ For every loaded .class file, JVM immediately creates an object on the
heap memory of type java.lang.class. Do remember, even though the
developers call a class multiple time, only one class object will be created.
Components of JVM
▰ 1) Classloader
▰ There are three built-in classloaders in Java.

▰ Bootstrap ClassLoader: This is the first classloader which is the super class
of Extension classloader. It loads the rt.jar file which contains all class files of
Java Standard Edition like java.lang package classes, java.net package
classes, java.util package classes, java.io package classes, java.sql package
classes etc.
▰ Extension ClassLoader: This is the child classloader of Bootstrap and parent
classloader of System classloader. It loades the jar files located
inside $JAVA_HOME/jre/lib/ext directory.
▰ System/Application ClassLoader: This is the child classloader of Extension
classloader. It loads the classfiles from classpath. By default, classpath is set to
current directory. You can change the classpath using "-cp" or "-classpath"
switch. It is also known as Application classloader.
Memory Area Allocated by JVM

▰ 2) Class(Method) Area
▰ Class(Method) Area stores per-class structures such as the runtime constant pool,
field and method data, the code for methods.
▰ 3) Heap
▰ It is the runtime data area in which objects are allocated.
▰ 4) Stack
▰ Java Stack stores frames. It holds local variables and partial results, and plays a
part in method invocation and return.
▰ Each thread has a private JVM stack, created at the same time as thread.
▰ A new frame is created each time a method is invoked. A frame is destroyed when
its method invocation completes.
▰ 5) Program Counter Register
▰ PC (program counter) register contains the address of the Java virtual machine
instruction currently being executed.
Components of JVM (Cont,,,,)

▰ 6) Native Method Stack


▰ It contains all the native methods used in the application.
▰ 7) Execution Engine
▰ It contains:
▰ A virtual processor
▰ Interpreter: Read bytecode stream then execute the instructions.
▰ Just-In-Time(JIT) compiler: It is used to improve the performance. JIT compiles
parts of the byte code that have similar functionality at the same time, and hence
reduces the amount of time needed for compilation. Here, the term "compiler" refers to
a translator from the instruction set of a Java virtual machine (JVM) to the instruction
set of a specific CPU.
▰ 8) Java Native Interface (JNI)
▰ Java Native Interface (JNI) is a framework which provides an interface to communicate
with another application written in another language like C, C++, Assembly etc. Java
uses JNI framework to send output to the Console or interact with OS libraries.
▰ Linking: This component performs the linking of a class or an interface. As this component
involves the allocation of new data structures, it may throw the OutOfMemoryError and
performs the three important activities:
▻ Verification: It is a process of checking the binary representation of a class and
validating whether the generated .class file is valid or not. This process is performed by
the Bytecode verifier and if the generated .class file is not valid, a VerifyError is thrown
▻ Preparation: It is a process of assigning the memory for the class level or interface
level static variables and assigns the default values
▻ Resolution: It is a process of changing the symbolic references with the original
memory references from the method area
JVM parts (Contd..)

▰ Initialization: This component performs the final


phase of the class loading where all the static
variables are assigned the original values and the
static blocks are executed from the parent to the child
class.
▰ This process requires careful synchronization as JVM
is multithreaded and some threads may try to
initialize the same class or interface at the same time.
Java Class Loading in JVM
How ClassLoader works in Java?

▰ Classloader in Java works in three principles


i.e. Delegation, Visibility, and Uniqueness.
Write & Execute a Java Program

Open Notepad
Write java program
Compile your program
D:Amit>javac Welcome.java

Execute your class file


D:Amit>java Welcome

output:... Welcome to Java


How does JRE works?

To understand how the JRE works let us consider a Java source file saved as Welcome.java.
▰ The file is compiled into a set of Byte Code that is stored in a “.class” file. Here it will
be “Welcome.class“.

▰ The following actions occur at runtime.


▰ Class Loader
▰ The Class Loader loads all necessary classes needed for the execution of a program. It
provides security by separating the namespaces of the local file system from that
imported through the network. These files are loaded either from a hard disk, a
network or from other sources.
▰ Byte Code Verifier
▰ The JVM puts the code through the Byte Code Verifier that checks the format and
checks for an illegal code. Illegal code, for example, is code that violates access rights
on objects or violates the implementation of pointers.
The Java Execution Model
Java source code
Platform
Java compiler independent

Java byte-code

Byte-code Byte-code Java chip


interpreter compiler Java machine

CPU1 Native machine code


Platform
Java virtual machine dependent

CPU2
Java virtual machine
Java Program Execution

▰ The Byte Code verifier ensures that the code


adheres to the JVM specification and does not
violate system integrity.

▰ Interpreter
▰ At runtime the Byte Code is loaded, checked and run by the
interpreter. The interpreter has the following two functions:
▻ Execute the Byte Code
▻ Make appropriate calls to the underlying hardware

Complete Execution of Java Program
Interactions between JDK and JRE

▰ JVM becomes an
instance of JRE at
runtime of a Java
program.
▰ It is widely known as
a runtime interpreter.
▰ JVM largely helps in
the abstraction of
inner implementation
from the
programmers who
make use of libraries
for their programmes
from JDK.
JVM as an Interpreter and
Emulator
Interpreter & Emulator

▰ Usually, however, people do not have hardware Java


processor chips. They have ordinary PCs and Macintoshes.
▰ Now for the clever part: the Java processor can be
implemented as software! It is implemented as a program
that reads the bytecodes and performs the operations they
specify.
▰ This is another type of interpreter. Some interpreters run
source code written in a high level language like Basic;
others (like the Java interpreter) run bytecodes.

Interpreter & Emulator

▰ This second type of interpreter is sometimes called an emulator because it emulates hardware,


but in fact is software. 
▰ A Java bytecode interpreter can be created for any computer system. Once you have a Java
compiler and a Java interpreter you can run any Java program no matter what type of computer
you have.

Thank You !!

You might also like