Вы находитесь на странице: 1из 5

ASSIGNMENT #01

Q No :1What is JDK,JRE and JVM and what are the differences between them
?You may also use any figure/diagram to further elaborate your answer.

JDK
 The Java Development Kit (JDK) is a software development environment used
for developing Java applications and applets.
 JDK includes the Java Runtime Environment (JRE), an interpreter/loader
(Java), a compiler (javac), Javadoc and other tools needed in Java development.
 JDK and JRE(Java Runtime Environment) both contains JVM so that we can
run our java program.

JRE
 JRE stands for “Java Runtime Environment” and may also be written as “Java
RTE.”
 The Java Runtime Environment provides the minimum requirements for
executing a Java application; it consists of the Java Virtual Machine (JVM), core
classes, and supporting files.
 JDK(Java Development Kit) is for development purpose whereas JRE is for
running the java programs.

JVM
 JVM (Java Virtual Machine) is an abstract machine. It is called a
virtual machine because it doesn't physically exist.
 A specification where working of Java Virtual Machine is specified. But
implementation provider is independent to choose the algorithm.
 Runtime Instance Whenever you write java command on the command prompt
to run the java class, an instance of JVM is created.
 JVM(Java Virtual Machine) is the heart of java programming language and
provides platform independence.

Difference between JDK, JRE and JVM

JDK = JRE + Development Tool


JRE = JVM + Library Classes

JDK

Q No:2 What do we mean by the terms Portability and Platform


Independence? How is Java platform independent?
Ans: Portability is defined as the moving instructions in one language
from one operating environment to another operating environment.
 It allows to develop programs irrespective of hardware.
 For example, C, C++, Java are portable language whereas source
code, bytecode, machine code of Java is non-portable.
While Platform independence is the ability of a software to run on various
platforms, usually achieved by the use of running the code over virtual
machines in case of java it is JVM.
 If the application's compile code can run in any other operating
system then that application is called platform-independent.
 It is required in distributed application.
How is Java Platform Independent:
Platform independent language means once compiled you can execute the
program on any platform (OS).
Because the Java compiler converts the source code to bytecode, which is
Intermediate Language. Bytecode can be executed on any platform (OS)
using JVM (Java Virtual Machine).

Q No:3 Explain the Java program’s compilation and execution


process?
Ans: Compilation:
First, the source ‘.java’ file is passed through the compiler, which then
encodes the source code into a machine independent encoding, known as
Bytecode. The content of each class contained in the source file is stored
in a separate ‘.class’ file.
While converting the source code into the bytecode, the compiler follows
the following steps:
 Parse: Reads a set of *.java source files and maps the resulting token sequence
into AST (Abstract Syntax Tree)-Nodes.
 Enter: Enters symbols for the definitions into the symbol table.
 Process annotations: If Requested, processes annotations found in the
specified compilation units.
 Attribute: Attributes the Syntax trees. This step includes name resolution, type
checking and constant folding.
 Flow: Performs dataflow analysis on the trees from the previous step. This
includes checks for assignments and reachability.
 DE sugar: Rewrites the AST and translates away some syntactic sugar.
 Generate: Generates ‘.Class’ files.
Execution:
The class files generated by the compiler are independent of the machine
or the OS, which allows them to be run on any system.
To run, the main class file (the class that contains the method main) is
passed to the JVM, and then goes through three main stages before the
final machine code is executed. These stages are:
1. Class Loader: A class loader, itself an object, creates a flat name
space of class bodies that are referenced by a string name.
The method definition is:

// loadClass function prototype

Class r = loadClass(String className, boolean resolveIt);


// className: name of the class to be loaded

2. Bytecode Verifier: After the bytecode of a class is loaded by the


class loader, it has to be inspected by the bytecode verifier, whose
job is to check that the instructions don’t perform damaging actions.
The following are some of the checks carried out:
 Variables are initialized before they are used.
 Method calls match the types of object references.
 The run time stack does not overflow.

If any of the above checks fails, the verifier doesn’t allow the class to
be loaded.

3. Just-In-Time Compiler: This is the final stage encountered by the


java program, and its job is to convert the loaded bytecode into
machine code.
When using a JIT compiler, the hardware can execute the native
code, as opposed to having the JVM interpret the same sequence of
bytecode repeatedly and incurring the penalty of a relatively lengthy
translation process.
The process can be well-illustrated by the following diagram:
Q No:4 Suppose you get a new Windows PC for Java programming. You need to
have JDK and JRE present to develop and execute your java programs. How will
you check the following?
a. Whether JDK is installed? Which version of JDK is there?
b. Whether JRE and JVM is present? Which version of JRE is there?
Ans:

Вам также может понравиться