Sie sind auf Seite 1von 8

Tatenda Mawaya H180298A Java programming assignment 1

Question 1

Java Virtual Machine

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

It is the runtime data area in which objects are allocated.

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.

Program Counter Register

PC (program counter) register contains the address of the Java virtual machine instruction
currently being executed.

Native Method Stack

It contains all the native methods used in the application.

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

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

Automatic Garbage Collection

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

protected void finalize(){}

Example

public class TestGarbage1{

public void finalize(){System.out.println("object is garbage collected");}

public static void main(String args[]){

TestGarbage1 s1=new TestGarbage1();

TestGarbage1 s2=new TestGarbage1();

s1=null;

s2=null;

System.gc();

Question4
Data Hiding and Access modifiers

Access Modifiers: public, private, protected

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

public class Countdown


Tatenda Mawaya H180298A Java programming assignment 1

private int ctr;

public Countdown(int start) { ctr = start; }

public String next()

if (ctr < 0) return null;

if (ctr == 0) { ctr--; return "Blastoff!"; }

return "" + ctr--;

Question 2
Java Code for Creating and Multiplying two 2x2 matrices

import java.util.Scanner;

class MatrixMultiplication

public static void main(String args[])

{
Tatenda Mawaya H180298A Java programming assignment 1

int m, n, p, q, sum = 0, c, d, k;

Scanner in = new Scanner(System.in);

System.out.println("Enter the number of rows and columns of first matrix");

m = in.nextInt();

n = in.nextInt();

int first[][] = new int[m][n];

System.out.println("Enter elements of first matrix");

for (c = 0; c < m; c++)

for (d = 0; d < n; d++)

first[c][d] = in.nextInt();

System.out.println("Enter the number of rows and columns of second matrix");

p = in.nextInt();

q = in.nextInt();

if (n != p)

System.out.println("The matrices can't be multiplied with each other.");

else

int second[][] = new int[p][q];

int multiply[][] = new int[m][q];

System.out.println("Enter elements of second matrix");


Tatenda Mawaya H180298A Java programming assignment 1

for (c = 0; c < p; c++)

for (d = 0; d < q; d++)

second[c][d] = in.nextInt();

for (c = 0; c < m; c++)

for (d = 0; d < q; d++)

for (k = 0; k < p; k++)

sum = sum + first[c][k]*second[k][d];

multiply[c][d] = sum;

sum = 0;

System.out.println("Product of the matrices:");

for (c = 0; c < m; c++)

for (d = 0; d < q; d++)

System.out.print(multiply[c][d]+"\t");

System.out.print("\n");
Tatenda Mawaya H180298A Java programming assignment 1

Das könnte Ihnen auch gefallen