Beruflich Dokumente
Kultur Dokumente
Question 1
JVM is an abstract computing machine or Virtual machine that resides in your RAM. It
has a platform-independent execution environment that interprets Java bytecode into
native machine code. (Javac is Java Compiler which compiles your Java code into
Bytecode)
Java program will be running inside the JVM which is then mapped onto the underlying
physical machine. It is one of programming tool in JDK.
Classloader
Classloader is a subsystem of JVM which is used to load class files. Whenever we run the java
program, it is loaded first by the 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.
Tatenda Mawaya H180298A Java programming assignment 1
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.
Class(Method) Area
Class(Method) Area stores per-class structures such as the runtime constant pool, field and
method data, the code for methods.
Heap
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.
PC (program counter) register contains the address of the Java virtual machine instruction
currently being executed.
Execution Engine
Just-In-Time(JIT) compiler: It is used to improve the performance. JIT compiles parts of the byte
Tatenda Mawaya H180298A Java programming assignment 1
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.
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.
Question3
Java garbage collection is the process by which Java programs perform automatic memory
management. Java programs compile to bytecode that can be run on a Java Virtual Machine, or
JVM for short. When Java programs run on the JVM, objects are created on the heap, which is a
portion of memory dedicated to the program. Eventually, some objects will no longer be
needed. The garbage collector finds these unused objects and deletes them to free up memory.
finalize() method
The finalize() method is invoked each time before the object is garbage collected. This method
can be used to perform cleanup processing. This method is defined in Object class as:
Tatenda Mawaya H180298A Java programming assignment 1
Example
s1=null;
s2=null;
System.gc();
Question4
Data Hiding and Access modifiers
Access modifiers allow the programmer to indicate what can be used (called, assigned to, read
from) by what parts of a program in a way that is enforced not just by the compiler but, more
importantly, by the JVM!
There are three basic modifiers: public, private, protected. Within the scope of the language as
we know it, only public and private are meaningful. The access modifier (if present) is the very
first thing in a declaration.
Example
Question 2
Java Code for Creating and Multiplying two 2x2 matrices
import java.util.Scanner;
class MatrixMultiplication
{
Tatenda Mawaya H180298A Java programming assignment 1
int m, n, p, q, sum = 0, c, d, k;
m = in.nextInt();
n = in.nextInt();
first[c][d] = in.nextInt();
p = in.nextInt();
q = in.nextInt();
if (n != p)
else
second[c][d] = in.nextInt();
multiply[c][d] = sum;
sum = 0;
System.out.print(multiply[c][d]+"\t");
System.out.print("\n");
Tatenda Mawaya H180298A Java programming assignment 1