CSI21M1 Java Programming Notes, prepared by Mr. S. Nyika, Semester I, 2019
Lecture 3: Java Applications and Memory Diagrams
Objectives:
At the end of this lecture, students will be able to:
• define the term install
• understand the JVM, JRE and JDK
• define the term variable
• understand the two kinds of memory available to programmes – stack and the heap
• Customize your netbeans IDE
• draw a memory diagram for an application
• distinquish between an instance variable and a static variable
Notes:
Definition: To install is simply to put. Examples are to install a vice chancellor, meaning to put the
vice chancellor in his office, to install a plate on the table, meaning to place or to put a plate on the
table. In our scenario in computer science the term install means to copy programs from a CD or from
the internet and place them in some folder. In most cases, this process is automated and one has to
click options and things just happen. However, it must be realized that installation is simply a process
of puting files in some folder.
JVM stands for Java Virtual Machine which is the component which enables programs to execute in
all types of machines. This means that the JVM takes the .class files (known as bytecode) and converts
them to machine code and CPU can execute them.
JRE is an acronym for Java Runtime Environment, it contains the JVM and Java Packages with
classes which come ready for use for Java developers like (java.util.*, javax.swing.*, java.lang.*, etc)
plus other supporting files. So JRE = JVM+Java Packages+other supporting files.
JDK stands for Java Development Kit (targeted for developers) which is just a folder containing all
the necesary software which makes the programmers compile java programs (obtaining the .class – the
bytecode), generate the documentation, debugg java programs, run java programs. These programs
are also called Java utilities. For example,
i) javac is a utility used for compiling java programs (converts the program into bytecode)
ii) java is a utility used to execute the programs (actually the JVM runs the program)
iii) javap is a utility used to reveal the API or Java documentation of classes (actually the JRE
contains the package classes)
iv) javadoc is a utility for generating java documentation
v) javadb is a java debbugger which allows the programmer to debug an application.
The JDK has versions and the latest version is JDK 1.7, we install the JDK by downloading the files
and copying them to some folder on our computer. On our linux computer the installation folder is the
folder /user./local/jdk1.7 and the utilities are in the subfolder called bin of this folder.
If you observe carefully, to be able to write a Java application and execute it, we need to install:
• a JRE (which contains a JVM inside it) and a JDK
• an editor or an Integrated Development Environment - IDE in which to write the Java Code.
(like NetBeans, Jgrasp, Eclipse)
Definition: A variable is a space in memory where we can store a value. This is labeled with a name
called a variable name e.g int x; x is the variable name and the actual space identified by x is not
visible to us.
x The variable – the space within memory
Page 1 of 16
, CSI21M1 Java Programming Notes, prepared by Mr. S. Nyika, Semester I, 2019
To create an object, we simple are making use of two kinds of memory, the stack and the heap. When
we declare primitive variables like the int , byte, float, double, char, long, these stay in stack memory
and that's it. When we use the objects, we use a special operator new, which (i) reserves memory in
heap memory and (ii) takes the address of that memory part and keeps in stack memory.
There are two main stackholders in application development which are interesting to look at, namely:
• The class creators – the ones who create class for use by others later. These make the classes
and create the documentation which provides the API for the classes they make. This hides the
implementation details of the class methods and shows only the method signatures and
explanations of how they work. The focus here is to expose the functionality, not the details of
what they do. This hiding of the method details will help class users not to mess up the data in
the class. The goal of the class creator is to build classes and expose only what is necessary to
the client programmer and keeps everything else hidden. Why? Because, the client
programmer could easily mess up the tender inside of an object.
• The class consumers – client programmers these are the ones who use up or consume the
classes made by others. They simply import packages which contain a group of classes already
made, they look at the API provided by the documentation and they begin to build applications
using these classes as building blocks. The goal of the client programmers is to get hold of a
toolbox full of classes and create an application quickly.
So there is a relationship here, and in any relationship, its important to create boundaries. This
way the class creators can change the inside of an object without worrying about the impact it has on
the client programmer. These boundaries are made possible by the use of the words private and public.
When you use the word private, you are putting a brick wall between you and the client programmer.
You are saying, this part is not for you, it is mine to play around with – so I am hiding it. The
public portions are areas where you can play and do what you want.
Creation of Objects in java
Java uses dynamic memory allocation exclusively. Every time you want to create and object, you use
the new operator to build a dynamic instance of that object. You treat everything as an object using a
consistent syntax. Although you treat everything as an object, the identifier you manipulate is actually a
reference to an object. Imagine the television set (an object) and a remote control (a reference) as long
as you are holding this reference, you have a connection to the television. When someone says, change
the channel, or lower the volume, what you are manipulating is the reference (the remote control)
which in turn modifies the object. If you want to move around the room and still want to control the
television, you take the remote control with you and not the television. A remote control can stand on
its own without a television. It means that having a reference does not mean you have an object
connected to it. So if you want to hold a sentence or a word, you create a String reference. String
str;
But here, you have only created a reference, not an object. If you decided to send a message to an
object reference by str, you will get an error because it isn't attached to anything (there is no
television).
Page 2 of 16
, CSI21M1 Java Programming Notes, prepared by Mr. S. Nyika, Semester I, 2019
A safer practice is therefore to initialize a reference when you create it;
String str=””; or String str=new String(“”);
You can also operate your tv without the remote control. That is you can create the object with the
operator new but do not asign a references. In this case, you make sure you will not need to send
any messages to it. It will stay on one channel. You can do this with the statement as follows:
new HelloWorld(); if the class is called Helloworld(). (your new tv without remote control)
Where does the storage lives. (This is why we need memory diagrams which show the
memory state)
It is important to visualize how things are laid when the application is running – in particular, how
memory is organized. There are five different places in Computer Memory to store data.
1. Registers.
This is the fastest storage because it exists in a place different from that of other storage: inside
the processor- CPU. However, the number of registers is severely limited, so registers are
allocated as they are needed. You don’t have direct control, nor do you see any evidence in
your programs that registers even exist.
2. The stack.
This lives in the general random-access memory (RAM) area, but has direct support from the
processor via its stack pointer. The stack pointer is moved down to create new memory and
moved up to release that memory. This is an extremely fast and efficient way to allocate storage,
second only to registers. The Java system must know, while it is creating the program, the exact
lifetime of all the items that are stored on the stack. This constraint places limits on the
flexibility of your programs, so while some Java storage exists on the stack—in particular,
object references—Java objects themselves are not placed on the stack.
3. The heap.
This is a general-purpose pool of memory (also in the RAM area) where all Java objects live.
The nice thing about the heap is that, unlike the stack, the compiler doesn’t need to know
how long that storage must stay on the heap. Thus, there’s a great deal of flexibility in using
storage on the heap. Whenever you need an object, you simply write the code to create it by
using new, and the storage is allocated on the heap when that code is executed.
4. Constant storage.
Constant values are often placed directly in the program code, which is safe since they can
never change.
5. Non-RAM storage.
If data lives completely outside a program, it can exist while the program is not running,
outside the control of the program. The two primary examples of this are streamed objects, in
which objects are turned into streams of bytes, generally to be sent to another machine, and
persistent objects, in which the objects are placed on disk so they will hold their state even
when the program is terminated.
Page 3 of 16