Download as pdf or txt
Download as pdf or txt
You are on page 1of 80

A Guide to Advanced Java

Student Guide
A Guide to Advanced Java
Student Guide

© 2013 Aptech Limited

All rights reserved

No part of this book may be reproduced or copied in any form or by any means – graphic, electronic or
mechanical, including photocopying, recording, taping, or storing in information retrieval system or sent
or transferred without the prior written permission of copyright owner Aptech Limited.

All trademarks acknowledged.

APTECH LIMITED
Head Office:
Aptech House,
A-65, MIDC,
Andheri (East),
Mumbai - 400 093.

www.aptech-globaltraining.com
E-mail: customercare@aptech.ac.in

First Edition - 2013


Dear Learner,

We congratulate you on your decision to pursue an Aptech Worldwide course.

Aptech Ltd. designs its courses using a sound instructional design model – from conceptualization
to execution, incorporating the following key aspects:


 Scanning the user system and needs assessment

Needs assessment is carried out to find the educational and training needs of the learner

Technology trends are regularly scanned and tracked by core teams at Aptech Ltd. TAG*
analyzes these on a monthly basis to understand the emerging technology training needs for
the Industry.

An annual Industry Recruitment Profile Survey is conducted during August - October to


understand the technologies that Industries would be adapting in the next 2 to 3 years.
An analysis of these trends & recruitment needs is then carried out to understand the skill
requirements for different roles & career opportunities.

The skill requirements are then mapped with the learner profile (user system) to derive the
Learning objectives for the different roles.


 Needs analysis and design of curriculum

The Learning objectives are then analyzed and translated into learning tasks. Each learning
task or activity is analyzed in terms of knowledge, skills and attitudes that are required to
perform that task. Teachers and domain experts do this jointly. These are then grouped in
clusters to form the subjects to be covered by the curriculum.

In addition, the society, the teachers, and the industry expect certain knowledge and skills
that are related to abilities such as learning-to-learn, thinking, adaptability, problem solving,
positive attitude etc. These competencies would cover both cognitive and affective domains.

A precedence diagram for the subjects is drawn where the prerequisites for each
subject are graphically illustrated. The number of levels in this diagram is determined
by the duration of the course in terms of number of semesters etc. Using the precedence
diagram and the time duration for each subject, the curriculum is organized.

 Design & development of instructional materials

The content outlines are developed by including additional topics that are required for the
completion of the domain and for the logical development of the competencies identified.
Evaluation strategy and scheme is developed for the subject. The topics are arranged/organized
in a meaningful sequence.
The detailed instructional material – Training aids, Learner material, reference material, project
guidelines, etc.- are then developed. Rigorous quality checks are conducted at every stage.


 Strategies for delivery of instruction

Careful consideration is given for the integral development of abilities like thinking, problem
solving, learning-to-learn etc. by selecting appropriate instructional strategies (training
methodology), instructional activities and instructional materials.

The area of IT is fast changing and nebulous. Hence considerable flexibility is provided in the
instructional process by specially including creative activities with group interaction between the
students and the trainer. The positive aspects of Web based learning –acquiring information,
organizing information and acting on the basis of insufficient information are some of the
aspects, which are incorporated, in the instructional process.


 Assessment of learning

The learning is assessed through different modes – tests, assignments & projects. The
assessment system is designed to evaluate the level of knowledge & skills as defined by the
learning objectives.


 Evaluation of instructional process and instructional materials

The instructional process is backed by an elaborate monitoring system to evaluate - on-time


delivery, understanding of a subject module, ability of the instructor to impart learning. As an
integral part of this process, we request you to kindly send us your feedback in the reply pre-
paid form appended at the end of each module.

*TAG – Technology & Academics Group comprises of members from Aptech Ltd., professors from
reputed Academic Institutions, Senior Managers from Industry, Technical gurus from Software
Majors & representatives from regulatory organizations/forums.

Technology heads of Aptech Ltd. meet on a monthly basis to share and evaluate the technology
trends. The group interfaces with the representatives of the TAG thrice a year to review and
validate the technology and academic directions and endeavors of Aptech Ltd.
Aptech New Products Design Model

Key Aspects

Scanning the Evaluation of


user system Instructional
and needs Processes and
assessment Material

2 Need Analysis 6
and design of Assessment
curriculum of learning

3 Design and 5
Strategies for
development of delivery of
instructional instructions
material 4

A little learning is a
dangerous thing, “
but a lot of ignorance
is just as bad.
Preface

The book A Guide to Advanced Java serves as the reference of concepts and APIs of Java.

This book is the result of a concentrated effort of the Design Team, which is continuously striving to bring
you the best and the latest in Information Technology. The process of design has been a part of the ISO
9001 certification for Aptech-IT Division, Education Support Services. As part of Aptech’s quality drive,
this team does intensive research and curriculum enrichment to keep it in line with industry trends.

We will be glad to receive your suggestions. Please send us your feedback, addressed to the Design
Centre at Aptech’s corporate office, Mumbai.

Design Team


Nothing is a waste of time if you
use the experience wisely
Table of Contents

Sessions
1. Introduction to Threads 1

2. More on Threads 11

3. The java.lang Package 25

4. java.io Package 39

5. java.util and Collections API 59



“Any expansion is life,
all contraction is death
1
Session

Introduction to
Threads

Concepts
Objectives

At the end of this session, you will be able to:

 Define processes and threads

 Compare and contrast processes and threads

 Describe support for threads in Java

 Describe methods of Thread class

 List the overloaded versions of sleep() method

1.1 Introduction

An application typically consists of one or more processes. A process, in the most basic terms, is an
executing program. One or more threads usually run within a process. A thread is nothing but the basic
unit to which the operating system allocates processor time. A thread is the entity within a process that
can be scheduled for execution. A process is started with a single thread, often called the primary, default
or main thread. Additional threads can be created programmatically as and when necessary.

1.1.1 Comparing Processes and Threads

Multiple threads allow access to the same part of memory whereas processes cannot directly access
memory of another process.

Some of the similarities and differences of processes and threads are:

 Similarities

 Threads share a central processing unit and only one thread is active (running) at a time.

 Threads within processes execute sequentially.

 A thread can create child threads or sub threads.

 If one thread is blocked, another thread can run.

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page  of 68


Session 1
Introduction to Threads

 Differences
Concepts

 Unlike processes, threads are not independent of one another.

 Unlike processes, all threads can access every address in the task.

 Unlike processes, threads are designed to assist one other.

1.2 Creating and Using Threads in Java

Java provides support for threaded applications using the Thread class and the Runnable interface. A
thread can be created either by deriving a class from the java.lang.Thread class or by implementing
the java.lang.Runnable interface. The first approach fails when a class that wants to implement
thread is being derived from another class. The second approach can be used in this case because Java
does not allow multiple class inheritance. Therefore, depending upon the need and requirement, either
of the approaches can be used to create Java threads. In the first approach the run() method is not
necessary to be implemented. In the second approach a class that implements the Runnable interface
must implement run() method.

Constructors of the Thread class are listed in Table 1.1. The ThreadGroup class represents a group of
threads and is often used in constructors of the Thread class.

Constructor Signature Description


Thread() Default constructor
Thread(Runnable objRun) Creates a new Thread object, where objRun is the
object whose run() method is called
Thread(Runnable objRun, String threadName) Creates a new named Thread object, where
objRun is the object whose run() method is called
and threadName is the name of the thread that will
be created
Thread(String threadName) Creates a new Thread object where threadName
is the name of the thread that will be created
Thread(ThreadGroup group, Runnable objRun) Creates a new Thread object, where group is the
thread group and objRun is the object whose run()
method is called
Thread(ThreadGroup group, Runnable objRun, Creates a new Thread object so that it has objRun
String threadName) as its run object, has the specified threadName as
its name, and belongs to ThreadGroup referred to
by group
Thread(ThreadGroup group, Runnable objRun, Creates a new Thread object so that it has objRun
String threadName, long stackSize) as its run object, has the specified threadName as
its name, belongs to the thread group referred to
by group, and has the specified stack size

page  of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 1
Introduction to Threads

Constructor Signature Description

Concepts
Thread(ThreadGroup group, String threadName) Creates a new Thread object with group as the
thread group and threadName as the name of the
thread that will be created

Table 1.1: Constructors of Thread Class

Example 1 demonstrates how a thread can be created using the interface Runnable.

Example 1:

/*
*Creating threads using Thread
*class and using methods of the class
*/
package test;
/**
* NamedThread is created so as to implement the interface Runnable
*/
class NamedThread implements Runnable {
/* this will store name of the thread */
String name;
/**
* This method of Runnable is implemented to specify the action
* that will be done when the thread begins execution.
*/
public void run() {
int count = 0; //will store the number of threads
while(count < 3){
name = Thread.currentThread().getName();
System.out.println(name);
count++;
}
}
}

public class Main {


public static void main(String args[]){
NamedThread objNewThread= new NamedThread();

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page  of 68


Session 1
Introduction to Threads

Thread objThread = new Thread(objNewThread);


Concepts

objThread.start();
}
}

In this example, the NamedThread class implements Runnable interface. The NamedThread class
implements Runnable, therefore its instance can be passed as an argument to the constructor of Thread
class. The output will be:
Thread-0
Thread-0
Thread-0

1.2.1 Methods of Thread Class

The Thread class provides a number of methods to work with threaded programs. Some methods of the
Thread class are listed in Table 1.2.

Method Names Description


static int activeCount() Returns the number of active threads among the current threads
in the program
static Thread currentThread() Returns a reference to the currently executing thread object
ThreadGroup getThreadGroup() Returns the thread group to which this thread belongs
static boolean interrupted() Tests whether the current thread has been interrupted
boolean isAlive() Tests if this thread is alive
boolean isInterrupted() Tests whether this thread has been interrupted
void join() Waits for this thread to die
void setName(String name) Changes the name of this thread to be equal to the argument
name

Table 1.2: Methods of the Thread Class

Example 2 demonstrates creating a new thread by extending Thread class and using some of the
methods of Thread class.

Example 2:

/**
* Creating threads using Thread class and using methods of the class
*/
package demo;

page  of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 1
Introduction to Threads

/**

Concepts
* NamedThread is created as a subclass of the class Thread
*/
public class NamedThread extends Thread {

/* This will store name of the thread */


String name;

/**
* This method of Thread class is overridden to specify the action
* that will be done when the thread begins execution.
*/
public void run() {
//Will store the number of threads
int count = 0;
while(count<=3) {

//Display the number of threads


System.out.println(Thread.activeCount());

//Display the name of the currently running thread


name = Thread.currentThread().getName();
count++;
System.out.println(name);
if (name.equals (“Thread1”))
System.out.println(“Marimba”);
else
System.out.println(“Jini”);
}
}

public static void main(String args[]) {


NamedThread objNamedThread = new NamedThread();
objNamedThread.setName(“Thread1”);

//Display the status of the thread, whether alive or not


System.out.println(Thread.currentThread().isAlive());

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page  of 68


Session 1
Introduction to Threads

System.out. println(objNamedThread.isAlive());
Concepts

/*invokes the start method which in turn will call


* run and begin thread execution
*/
objNamedThread.start();
System.out.println(Thread.currentThread().isAlive());
System.out.println(objNamedThread.isAlive());
}
}

In this example, NamedThread is declared as a derived class of Thread. In the main() method of this
class, a thread object objNamedThread is created by instantiating NamedThread and its name is set
to Thread1. The code then checks to see if the current thread is alive by invoking the isAlive() method
and displays the return value of the method. This will result in true being printed because the main
(default) thread has begun execution and is currently alive. The code also checks if objNamedThread
is alive by invoking the same method on it. However, at this point of time, objNamedThread has not yet
begun execution so the output will be false. Next, the start() method is invoked on objNamedThread
which will cause the thread to invoke the run() method which has been overridden. The run() method
prints the total number of threads running, which are by now, 2. The method then checks the name of
the thread running and prints Marimba if the currently running thread’s name is Thread1. The method
performs this checking three times. Thus, the final output of the code will be:
true
false
true
true
2
Thread1
Marimba
2
Thread1
Marimba
2
Thread1
Marimba
2
Thread1
Marimba

page  of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 1
Introduction to Threads

1.2.2 The sleep Method

Concepts
There are two overloaded versions of the sleep() method which allow a thread to sleep for a certain
period.

 public static void sleep (long millis)

It throws an InterruptedException, which should be caught by a corresponding catch block.


The method sends the currently executing thread to a blocked state for the specified number of
milliseconds given as argument.

 public static void sleep (long millis, int nanos)

This method also throws InterruptedException, which should be caught by a corresponding


catch block. It sends the currently executing thread to a blocked state for the specified number of
milliseconds plus the specified number of nanoseconds.

1.2.3 Life Cycle of a Thread

A thread can exist in a particular state such as ready, waiting, sleeping and so on. A thread’s transition
from one state to another can be caused by a method call performed by the thread itself, by a method call
possibly performed by another thread and by waiting timeouts and other actions.

The life cycle of a Java thread can be roughly represented as shown in Figure 1.1.

Figure 1.1: Lifecycle of a Thread

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page  of 68


Session 1
Introduction to Threads

1.3 Exceptions During Thread Processing


Concepts

While creating and working with threads, certain exceptions may occur. IllegalThreadState and
InterruptedException are two commonly encountered exceptions while working with threads. The
InterruptedException exception occurs when the interrupt() method is called on a thread
that is either waiting, sleeping or paused for a long time. Typically one thread calls interrupt()
on another thread to interrupt or wake it. This exception is a subclass of Exception class. The
IllegalThreadStateException occurs when a thread is not in a state to receive a request that has
been made for a specific operation.

Example 3 shows how the InterruptedException can be used with the Thread.sleep() method
to generate a series of random numbers.

Example 3:

/*
*Prints a series of random numbers
*/
public class RandomSeries {
public static void main(String[] args) {
while (true){
java.util.Random random = new java.util.Random();
System.out.println(((random.nextInt()/20000)>>>1)/1500);
try {
Thread.sleep(500);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}

In Example 3, an instance of the Random class is created and the nextInt() method is invoked on it.
Since, the series is to be generated infinitely with a delay of 500 milliseconds for each output until the
user presses Ctrl Break to abort the program, the code makes use of the Thread.sleep() method
and the InterruptedException exception. When the user interrupts the program execution, the
interrupt() method will be thrown by the code which will be handled by the catch block.

1.4 Managing Threads

In our day-to-day activities, different degrees of importance needs to be assigned to different tasks. The
decision is taken so that the best possible outcome is achieved. For example, while deciding between

page  of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 1
Introduction to Threads

the activities going to work and watching television, the activity going to work gets priority over watching
television. This is because going to work is more important for the financial benefit of the person than

Concepts
mere entertainment.

Similarly, in Java programming, it is necessary to prioritize the threads according to its importance. In a
thread based Java program, a number of threads are to be executed. The gaining of control over the time
slices for a thread execution should be according to the priority of the threads to use it. This prioritizing
process is managed by the scheduler which assigns priority to the respective threads. The managing of
threads is done by methods like setPriority() and getPriority().

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page  of 68


Session 1
Introduction to Threads

SUMMARY
Concepts

 A thread is the basic unit to which the operating system allocates processor time and which
can run in the context of a process.

 A process starts with a single thread, often called the primary, default or main thread, but
can also create additional threads from any of its threads.

 Java provides support for threaded applications through the Thread class and the Runnable
interface.

 The Thread class has many overloaded constructors and several methods to work with
threads.

 The Runnable interface has just one method run() - which must be implemented by any
class that implements the interface.

 The managing of threads is done by methods like setPriority() and getPriority().

page 10 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


2
Session

More on Threads

Concepts
Objectives

At the end of this session, you will be able to:

 Define and describe multithreading

 Explain isAlive() and join() methods

 Describe thread synchronization

 Explain wait-notify mechanism

 Describe how deadlocks occur and how they can be avoided

2.1 Introduction

Human beings are used to doing multiple tasks at a time. For instance, a person, while watching a
television show, can also attend a phone call and sip coffee at the same time. This kind of process where
multiple or several tasks can happen concurrently is called multitasking. A juggler juggling several balls
is a classic case of multitasking in real life.

Figure 2.1: Example of Multitasking in Real Life

Operating systems make extensive use of multitasking to provide efficient responses and faster processing
to a user. There are basically two types of multitasking in use among operating systems. These are:

 Preemptive

In this case, the operating system controls multitasking by assigning the CPU time to each running

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 11 of 68


Session 2
More on Threads

program. This approach is used in Windows 95 and 98.


Concepts

 Cooperative

In this approach, related applications voluntarily surrender their time to one another. This was used
in Windows 3.x.

Multithreading is a technique similar to multitasking and involves the creation of one or more threads
within a program to enable number of tasks to run concurrently or in parallel.

Multithreading also supports the following features:

 Managing many tasks concurrently.

 Distinguishing between tasks of varying priority.

 Allowing the user interface to remain responsive, while allocating time to background tasks.

2.1.1 Multithreading versus Multitasking

Multitasking is the ability of the operating system to run two or more processes or programs at the same
time (although in reality, they are not actually running at the same time, they are given time slices of the
CPU but the delays between each program is so less that it appears as if all are executing simultaneously).
Whereas, multithreading is the ability of an operating system to simultaneously run parts of a program as
each thread. Individual programs run by an operating system are typically independent of each other and
isolated in terms of their memory and data but individual threads are not isolated from each other as they
may share the same memory. Multithreading requires less overhead than multitasking.

The Java programming language provides ample support for multithreading by means of the Thread
class and Runnable interface. The code in Example 1 creates multiple threads, displays the count of the
threads and displays the name of each running child thread within the run() method.

Example 1:

/**
* Creating multiple threads using a class derived from Thread class
*/
package test;
/**
* MultipleThreads is created as a subclass of the class Thread
*/

page 12 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 2
More on Threads

public class MultipleThreads extends Thread {

Concepts
/* Variable to store the name of the thread */
String name;

/**
* This method of Thread class is overridden to specify the action
* that will be done when the thread begins execution.
*/
public void run() {
while(true) {
name = Thread.currentThread().getName();
System.out.println(name);
try {
Thread.sleep(500);
} catch(InterruptedException e) {
break;
}
}
}
/**
* This is the entry point for the MultipleThreads class.
*/
public static void main(String args[]) {
MultipleThreads t1 = new MultipleThreads();
MultipleThreads t2 = new MultipleThreads();
t1.setName(“Thread2”);
t2.setName(“Thread3”);
t1.start();
t2.start();
System.out.println(“Number of threads running: ” + Thread.
activeCount());
}
}

In the code shown in Example 1, the main() method creates two child threads by instantiating the
MultipleThreads class which has been derived from the Thread class. The names of the child
threads are set to Thread2 and Thread3 respectively. When the start() method is invoked on the
child thread objects, the control is transferred to the run() method which will begin thread execution. Just
as the child threads begin to execute, the number of active threads is printed in the main() method.

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 13 of 68


Session 2
More on Threads

The output of Example 1 will be:


Concepts

Number of threads running: 3


Thread2
Thread3
Thread2
Thread3
. . .

until the user presses Ctrl + C to stop execution of the program.

Thread execution stops once it finishes the execution of run() method. Once stopped, thread execution
cannot restart by using the start() method. Also, the start() method cannot be invoked on an
already running thread. This will also throw an exception of type IllegalThreadStateException.

Threads eat up a lot memory (RAM) therefore it is always advisable to set the references to null when a
thread has finished executing. If a Thread object is created but fails to call the start() method, it will
not be eligible for garbage collection even if the underlying application has removed all references to the
thread.

2.2 Using isAlive() and join() Methods

The thread that is used to start the application should be the last thread to terminate. This indicates that
the application has terminated. This can be ensured by stopping the execution of the main thread for a
longer duration within the main thread itself. Also it should be ensured that all the child threads terminate
before the main thread. However, how does one ensure that the main thread is aware of the status of the
other threads? There are two ways to find out if a thread has terminated. First, by using the isAlive()
method and second by using the join() method, that is used more frequently.

The Thread class uses the isAlive() method, which can be called on an instance of the thread,
to determine if it is currently executing. A thread is said to be alive if it has been started and has not yet
terminated (died). If isAlive() returns false, the thread is either a new thread that is not currently
executing or it is dead.

The join() method of the Thread class has three overloaded versions:

 void join()

In this type of join() method, no argument is passed. This forces the thread to wait till it dies.

 void join(long timeout)

In this type of join() method, an argument of type long is passed. The amount of timeout is in

page 14 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 2
More on Threads

milliseconds. This forces the thread to wait for the completion of the specified thread until the given
number of milliseconds elapses.

Concepts
 void join(long timeout, int nanoseconds)

In this type of join() method arguments of type long and integer are passed. The amount
of timeout is given in milliseconds in addition to a specified amount of nanoseconds. This forces the
thread to wait for the completion of the specified thread until the given timeout elapses.

Example 2:

/*
* Using the isAlive and join methods
*/
package test;

/** ThreadDemo inherits from Runnable interface */


class ThreadDemo implements Runnable {
String name;
Thread objTh;

/* Constructor of the class */


ThreadDemo(String str) {
name = str;
objTh = new Thread(this, name);
System.out.println(“New Threads are starting : “ + objTh);
objTh.start();
}

public void run() {


try {
for (int count = 0;count < 2;count++) {
System.out.println(name + “ : “+count);
objTh.sleep(1000);
}
} catch(InterruptedException e) {
System.out.println(name + “ interrupted”);
}
System.out.println(name + “ exiting”);
}

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 15 of 68


Session 2
More on Threads

public static void main(String [] args) {


Concepts

ThreadDemo objNew1 = new ThreadDemo(“one”);


ThreadDemo objNew2 = new ThreadDemo (“two”);
ThreadDemo objNew3 = new ThreadDemo (“three”);
System.out.println(“First thread is alive :” + objNew1.objTh.
isAlive());
System.out.println(“Second thread is alive :” + objNew2.objTh.
isAlive());
System.out.println(“Third thread is alive :” + objNew3.objTh.
isAlive());
try {
System.out.println(“In the main method, waiting for the threads
to finish”);
objNew1.objTh.join();
objNew2.objTh.join();
objNew3.objTh.join();
} catch(InterruptedException e) {
System.out.println(“Main thread is interrupted”);
}

System.out.println(“First thread is alive :” + objNew1.objTh.


isAlive());
System.out.println(“Second thread is alive :” + objNew2.objTh.
isAlive());
System.out.println(“Third thread is alive :” + objNew3.objTh.
isAlive());
System.out.println(“Main thread is over and exiting”);
}
}

In Example 2, three thread objects are created in the main() method. The isAlive() method is
invoked by the three thread objects to test whether they are alive or dead. Then the join() method is
invoked by each of the thread objects. The join() method ensures that the main thread is the last one
to terminate. Finally, the isAlive() method is invoked again to check whether the threads are still alive
or dead. These statements are enclosed inside a try-catch block.

The output for Example 2 is as follows:


New Threads are starting : Thread[one,5,main]
New Threads are starting : Thread[two,5,main]
New Threads are starting : Thread[three,5,main]
First thread is alive :true

page 16 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 2
More on Threads

Second thread is alive :true


Third thread is alive :true

Concepts
In the main method, waiting for the threads to finish
one : 0
two : 0
three : 0
one : 1
two : 1
three : 1
one exiting
two exiting
three exiting
First thread is alive :false
Second thread is alive :false
Third thread is alive :false
Main thread is over and exiting

2.3 Thread Synchronization

Consider a situation where people are standing in a queue outside a telephone booth. They wish to make
a call and are waiting for their turn. This is similar to a synchronized way of accessing data where each
thread wanting to access data waits its turn. However, going back to the analogy described earlier, if
there was no queue and people were permitted to go in randomly, two or more persons would try to enter
the booth at the same time, resulting in confusion and chaos. This is similar to a race condition that can
take place with threads. When two threads attempt to access and manipulate the same object and leave
the object in an undefined state, a race condition occurs. Java provides the synchronized keyword to
help avoid such situations. The core concept in synchronization with Java threads is something called
a monitor. A monitor is a piece of code that is guarded by a mutual-exclusion program called a mutex.
A real-life analogy for a monitor can be the telephone booth described earlier, but this time with a lock.
Only one person can be inside the telephone booth at a time and while the person is inside, the booth will
be locked, thus preventing the others from entering. The telephone inside the booth here is the real-life
equivalent of an object, the booth is the monitor and the lock is the mutex. A Java object has only one
monitor and mutex associated with it. A single monitor can have more than one door leading into it, each
of which is indicated by the synchronized keyword. When a thread encounters the synchronized
keyword, it locks all the doors on that object, preventing other threads from accessing it. There is no class
called Monitor in Java, rather each object has its own implicit monitor that is automatically entered when
any of the object’s synchronized methods is called.

For a thread to enter the monitor of an object, the programmer must invoke a method that has been
created using the synchronized keyword. As long as a thread executes within a synchronized method,
any other thread or synchronized method that tries to call it would have to wait.

Example 3 shows how to use a synchronized method.

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 17 of 68


Session 2
More on Threads

Example 3:
Concepts

/**
* Demonstrating synchronized methods.
*/
package test;

class One {

// This method is synchronized to use the thread safely


synchronized void display(int num) {
System.out.print(“”+num);
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
System.out.println(“Interrupted”);
}
System.out.println(“ done”);
}
}

class Two implements Runnable {


int number;
One objOne;
Thread objTh;

public Two(One one_num, int num) {


objOne = one_num;
number = num;
objTh = new Thread(this);
objTh.start();
}
public void run() {
// Invoke the synchronized method
objOne.display(number);
}
}

page 18 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 2
More on Threads

class SynchMethod {

Concepts
public static void main(String args[]) {
One objOne = new One();
int digit = 10;

// Create three thread objects


Two objSynch1 = new Two(objOne,digit++);
Two objSynch2 = new Two(objOne,digit++);
Two objSynch3 = new Two(objOne,digit++);

// Wait for the threads to end


try {
objSynch1.objTh.join();
objSynch2.objTh.join();
objSynch3.objTh.join();
} catch(InterruptedException e) {
System.out.println(“Interrupted”);
}
}
}

Here, the class One has a method display() that takes an int parameter. This number is displayed
with a suffix “done”. The Thread.sleep(1000) method pauses the current thread after the method
display() is called.

The constructor of the class Two takes a reference to an object t of the class One and an integer variable.
Here, a new thread is also created. This thread calls the method run() of the object t. The main class
SynchDemo instantiates the class One as a object objOne and creates three objects of the class Two.
The same object objOne is passed to each Two object. The method join() makes the caller thread
wait till the calling thread terminates.

The output of the code in Example 3 would be:


10 done
11 done
12 done

It is not always possible to achieve synchronization by creating synchronized methods within classes.
The reason for this is as follows.

Consider a case where the programmer wants to synchronize access to objects of a class, which does
not use synchronized methods. Also assume that the source code is unavailable because either a

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 19 of 68


Session 2
More on Threads

third party created it or the class was imported from the built–in library. In such a case, the keyword
synchronized cannot be added to the appropriate methods within the class.
Concepts

Therefore, the problem here would be how to make the access to an object of this class synchronized.
This could be achieved by putting all calls to the methods defined by this class inside a synchronized
block.

The general form of the synchronized statement is given by,

synchronized(<object>)
{
// statements to be synchronized
}

where,

<object> is the object being synchronized.

If a single statement is to be synchronized, then the curly braces are not required. A synchronized block
ensures that a method can be invoked only after the current thread has successfully entered object’s
monitor.

2.4 Wait and Notify Mechanism

Java also provides a wait and notify mechanism to work in conjunction with synchronization. The wait-
notify mechanism acts as the traffic signal system in the program. It allows the specific thread to wait for
some time for other running thread and wakes it up when it is required to do so. For these operations, it
uses the wait(), notify() and notifyAll() methods.

The notifyAll() method wakes up all the threads which are previously forced to wait.

These methods are declared as final methods in the Object class. As final methods cannot be
overridden by a subclass, these methods are called from synchronized blocks. The following table
lists the methods:

Method Name Description


wait() Waits forever until a notify() is sent.
wait(long interval) Waits interval number of seconds for a notify() and then resumes
execution.
wait(long interval, int nano) Waits interval seconds and nano nanoseconds for a notify() then
resumes execution.

page 20 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 2
More on Threads

Method Name Description

Concepts
notify() Sends a notification to a thread that is waiting on this object. If more
than one thread is waiting, it sends notification to the thread that
has waited longest.
notifyAll() Sends a notification to every thread that is waiting on this object.

Table 2.1: Wait and Notify Methods

Points to remember while using the wait() method:

 The calling thread gives up the CPU.

 The calling thread gives up the lock.

 The calling thread goes into the waiting pool of the monitor.

Points to remember while using the notify() method:

 One thread moves out of the waiting pool of the monitor and into the ready state.

 The thread that was notified must re-acquire the monitor’s lock before it can proceed. This is
because the thread before being notified was in the sleep state and therefore no longer had the
control of the monitor.

The Dining Philosophers problem is a legendary problem that demonstrates synchronization. The
problem consists of five philosophers sitting at a table who think and when they feel hungry, they eat.
Between each philosopher, there is a single chopstick. In order to eat, a philosopher must have two
chopsticks. A problem can arise if each philosopher grabs the stick on the right, then waits for the stick on
the left. In such a case a deadlock will occur and all philosophers will starve.

There are many implementations of this problem. It can be solved in many different ways. The code
for the legendary dining philosopher’s problem is shown in Example 4. It makes use of wait() and
notify() methods.

2.5 Deadlocks

A deadlock occurs when two threads have a circular dependency on a pair of synchronized objects.
For example, it is a situation when one thread enters the monitor on object ObjA and another thread
enters the monitor on object ObjB. If the thread in ObjA attempts to call any synchronized method on
ObjB, a deadlock occurs.

It is difficult to debug a deadlock since it occurs very rarely. Example 4 demonstrates a deadlock
condition.

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 21 of 68


Session 2
More on Threads

Example 4:
Concepts

/**
* Demonstrating Deadlock.
*/
package test;

/**
* DeadlockDemo implements the Runnable interface.
*/
public class DeadlockDemo implements Runnable {

public static void main(String args[]) {

DeadlockDemo objDead1 = new DeadlockDemo();


DeadlockDemo objDead2 = new DeadlockDemo();
Thread objTh1 = new Thread (objDead1);
Thread objTh2 = new Thread (objDead2);
objDead1.grabIt = objDead2;
objDead2.grabIt = objDead1;
objTh1.start();
objTh2.start();
System.out.println(“Started”);
try {
objTh1.join();
objTh2.join();
} catch(InterruptedException e) {
System.out.println(“error occurred”);
}
System.exit(0);
}
DeadlockDemo grabIt;

public synchronized void run() {


try {
Thread.sleep(500);
} catch(InterruptedException e) {

page 22 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 2
More on Threads

System.out.println(“error occurred”);

Concepts
}
grabIt.syncIt();
}
public synchronized void syncIt() {
try {
Thread.sleep(500);
System.out.println(“Sync”);
} catch(InterruptedException e) {
System.out.println(“error occurred”);
}
System.out.println(“In the syncIt() method”);
}
}// end class

The program creates two child threads. Each thread calls the synchronized run() method. When thread
objTh1 wakes up, it calls the method syncIt() of the DeadlockDemo object objDead1. Since the
thread objTh2 owns the monitor of objDead2, thread objTh1 begins waiting for the monitor. When
thread objTh2 wakes up, it tries to call the method syncIt() of the DeadlockDemo object objDead2.
At this point, objTh2 also is forced to wait since objTh1 owns the monitor of objDead1. Since both
threads are waiting for each other, neither will wake up. This is a deadlock condition. The program is
blocked and does not proceed further.

The output of the code is shown below:


Started

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 23 of 68


Session 2
More on Threads

SUMMARY
Concepts

 Data may get corrupted when two or more threads access the same variable or object at
the same time.

 The method join() will wait until the thread on which it is called terminates.

 Synchronization is a process that ensures that the resource will be used by only one thread
at a time.

 The method wait() tells the calling thread to give up the monitor and enter the sleep state till
some other thread enters the same monitor and calls the method notify().

 The method notify() wakes up or notifies the first thread that called wait() on the same
object.

 The method notifyAll() wakes up or notifies all the threads that called wait() on the same
object.

page 24 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


3
Session

The java.lang Package

Concepts
Objectives

At the end of this session, you will be able to:

 Describe the java.lang package

 Explain the important methods and properties of wrapper classes

 Explain the methods and properties of classes in the java.lang package

3.1 Packages in Java

Compilers require the definitions of the keywords and functions used in a program for compilation. By
default, the compiler searches for these definitions in the given source code. However, including the
definitions of all the keywords and functions inside the source code itself will make the program bulkier.
To avoid the program from getting bulkier, programming languages use the concept of library files. The
compiler can take the definitions of the classes and methods from these library files. This can also
facilitate reuse of the same code again and again instead of writing code all over once more.

In Java, the concept of library files is implemented using packages. A package is a collection of predefined
classes, interfaces and methods, which can be used in programs. All that a developer needs to do is
include a statement in the source code to import the required package. The Java Development Kit (JDK)
contains a host of off-the-shelf packages, such as java.lang, java.io and java.util. The classes
and interfaces available in the JDK are categorized based on the purpose and put into the respective
package. For example, the utility classes, such as Calendar and Dictionary, are available in the
java.util package.

3.1.1 What is the java.lang package?

The java.lang package is the default package that contains the definitions of basic classes and primitive
data types used in Java. Some of the important classes in this package are Thread, Object, Class,
Runtime, String, System and additionally, wrapper classes for primitive data types. This package
also contains interfaces, such as Comparable and Runnable, which are used to create multithreaded
programs.

3.2 Wrapper Classes

The java.lang package contains a group of classes known as wrapper classes, which represent the
primitive data types, such as int, float and double. These classes include Boolean, Byte, Integer,
Long, Short, Character, Float and Double. A typical wrapper class contains a value of primitive
data type and various methods for managing the data types.

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 25 of 68


Session 3
The java.lang Package

3.2.1 Boolean Class


Concepts

The Boolean class is associated with the primitive data type boolean. This class can be used to
create objects of type boolean and contains various methods for conversion and comparison of Boolean
objects.

The constructor for this class accepts a value of boolean type. An object of this class can be created using
a statement similar to the one given here:

Boolean objBoolean = new Boolean(true);

The Boolean class contains various methods for converting various types of data to boolean and vice
versa. These functions include booleanValue(), valueOf() and toString().

The booleanValue() method returns the value of the object as a primitive boolean type.

Code Snippet 1:

Boolean objBoolean = new Boolean(false);


if (objBoolean.booleanValue())
System.out.println(“It is true”);
else
System.out.println(“It is not true”);

Code Snippet 1 displays the following output:

It is not true

The method valueOf(boolean bool) also returns the boolean value of the object. The difference is
that the valueOf(boolean bool) does not require an object to be created when it is invoked. Hence,
the code consumes less memory and is faster.

Similarly, the method valueOf(String str) returns the boolean value of the string provided as the
argument. This method returns true if the argument is “true” and returns false if the argument is
“false”.

The toString() method returns the value of the Boolean object as a String value. This method is
used when one needs to store the value of a Boolean object to a String variable.

Code Snippet 2:

Boolean objBoolean = new Boolean(true);

page 26 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 3
The java.lang Package

String s = objBoolean.toString();

Concepts
System.out.println(“The value is “+s);

Code Snippet 2 displays the following output on compilation:

The value is true

The Boolean class contains two methods to compare one Boolean object with another. These functions
are compareTo(Boolean bool) and equals(Object obj).

The compareTo(Boolean bool) method compares a Boolean object with another and returns an
int value. The return value will be 0 if both the objects contain the same boolean value. It will return a
positive value if the first object is “true” and the second object is “false” and a negative value otherwise.

Code Snippet 3:

Boolean objBoolean1 = new Boolean(true);


Boolean objBoolean2 = new Boolean(false);
System.out.println(objBoolean1.compareTo(objBoolean2));

The output of Code Snippet 3 will be 1 because the value of objBoolean1 is true and the value of
objBoolean2 is false. Hence the comparison will be false.

The equals(Object obj) compares two Boolean objects and returns a boolean value true if the
objects are equal and false otherwise.

3.2.2 Byte Class

The Byte class is used to create objects of the primitive type byte. This class contains various methods
to manage Byte objects.

The constructor for this class accepts a value of byte or String type.
Byte(byte b)
Byte(String s)

The Byte class contains various methods to convert the byte type to various other types and vice versa.
Table 3.1 explains various conversion methods available in the Byte class.

Method Name Purpose


byteValue() Returns the primitive byte value in the Byte object
decode(String s) Converts the String argument to byte type

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 27 of 68


Session 3
The java.lang Package

Method Name Purpose


Concepts

doubleValue() Returns the value of the Byte object as a double value


floatValue() Returns the value of the Byte object as a float value

Table 3.1: Conversion Methods in the Byte Class

The comparison methods available in the Byte class are compareTo(Byte obj) and equals(Object
obj). The compareTo(Byte obj) method compares a Byte object with another and returns an int
value. The return value will be 0 if both the objects contain the same primitive byte value. The method
will return a positive value if the value in the first object is greater than that in the second object and a
negative value otherwise. The return value of the method is the numerical difference between the byte
values in the objects.

Code Snippet 4:

Byte objByte1 = new Byte ((byte) ’a’);


Byte objByte2 = new Byte ((byte) ’c’);
System.out.println(objByte1.compareTo(objByte2));

Code Snippet 4 will display -2, as the first object is lesser by 2 as compared to the second object.

The equals(Object obj) compares two Byte objects and returns a boolean value true if the objects
are equal and false otherwise.

3.2.3 Character Class

The Character class is associated with the primitive data type char. It is used to create objects of the
type char. This contains various methods for conversion and comparison of Character objects.

The constructor for this class accepts a value of char type. An object of this class can be created using
the following syntax:

Syntax:

Character objCharacter = new Character(‘A’);

Similar to other wrapper classes, the Character class also has conversion and comparison methods.
In addition, this class has various methods for identifying the types of character in the class. It can check
whether the character is a digit, a letter, a lower case alphabet or a space.

The Character class contains various methods to convert the char type to other types and vice versa.
These methods include charValue(), toString() and valueOf(char c). This class also contains
methods, such as toLowerCase(char c) and toUpperCase() to convert the casing of the alphabet.

page 28 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 3
The java.lang Package

Table 3.2 explains various conversion methods available in the Character class.

Concepts
Method Name Purpose
charValue() Returns the primitive char value in the Character object
toString (char c) Converts the Character object to a String value
valueOf(char c) Converts the char value to a Character object
toLowerCase(char c) Converts the char value to lowercase
toUpperCase(char c) Converts the char value to uppercase

Table 3.2: Conversion Methods in the Character Class

Code Snippet 5 demonstrates some of the conversion methods.

Code Snippet 5:

if (Character.toUpperCase(ch)==’A’)
System.out.println(“The character is A or a”);
else
System.out.println(“The character is not A or a”);

Code Snippet 5 will check if the value of the variable ch converted to upper case is ‘A’ and display the
message accordingly.

The comparison methods available in the Character class are compareTo(Character obj) and
equals(Object obj).

The compareTo(Character obj) method compares a Character object with another and returns
an int value. The return value will be 0 if both the objects contain the same primitive char value. It
will return a positive value if the value in the first object is greater than that in the second object and a
negative value otherwise. The return value of the method is the numerical difference between the char
values in the objects.

Code Snippet 6:

Character objCharacter1 = new Character(‘z’);


Character objCharacter2 = new Character(‘a’);
int diff = objCharacter1.compareTo(objCharacter2);
if (diff == 0)
System.out.println(“Both the characters are equal.”);
else

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 29 of 68


Session 3
The java.lang Package

System.out.println(“The characters are different and difference


is:”+diff);
Concepts

Code Snippet 6 displays 25, which is the numerical difference between the characters ‘z’ and ‘a’.

The equals(Object obj) compares two Character objects and returns a boolean value true if the
objects are equal and false otherwise.

In the Character class, there is a set of methods to check the data type and casing of the character in
an object. Table 3.3 explains various methods for checking various data types.

Method Name Purpose


isDigit(char ch) Returns true if the character is a digit and returns false otherwise
isLetter(char ch) Returns true if the character is a letter and returns false otherwise
isLowerCase(char ch) Returns true if the character is a lowercase alphabet and returns false
otherwise
isSpaceChar() Returns true if the character is a space and returns false otherwise

Table 3.3: Methods for Checking the Data Types

Code Snippet 7 demonstrates the use of some of the methods listed in Table 3.3.

Code Snippet 7:

String str = “221 B Bakers Street”;


for (int i=0;i<str.length();i++){
if (Character.isDigit(str.charAt(i)))
System.out.println(“Character at “+i+” is digit”);
else if (Character.isSpaceChar(str.charAt(i)))
System.out.println(“Character at “+i+” is space”);
else if (Character.isLetter(str.charAt(i)))
System.out.println(“Character at “+i+” is letter”);

Code Snippet 7 will display the type of each character in the string as a digit, space and letter
respectively.

3.2.4 Integer Class

The Integer class is associated with the primitive data type int. This class is used to create objects of
numeric type. It contains various methods for conversion and comparison of Integer objects.

page 30 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 3
The java.lang Package

The constructor for this class accepts a value of int or String type.

Concepts
Objects of this class can be created as shown:
Integer objInteger1 = new Integer(100);
Integer objInteger2 = new Integer(”100”);

As in other wrapper classes, the Integer class has conversion and comparison methods.

The Integer class contains various methods to convert the int type to other types and vice versa. Table
3.4 explains various conversion methods available in the Integer class.

Method Name Purpose


intValue() Returns the primitive int value in the Integer object
shortValue() Returns a primitive short value in the Integer object
longValue() Returns a primitive long value in the Integer object
floatValue() Returns a primitive float value in the Integer object
doubleValue() Returns a primitive double value in the Integer object
toString() Converts the Integer object to a String value
valueOf(String str) Converts the string value to an Integer object

Table 3.4: Conversion Methods in the Integer Class

Code Snippet 8 prints the float value of an integer.

Code Snippet 8:

Integer objInteger = new Integer(100);


float flNum;
flNum = objInteger.floatValue();
System.out.println(“Integers are “+objInteger+” and “+flNum);

Code Snippet 8 will convert the value in the Integer object to float and display the message
accordingly.

The comparison methods available in the Integer class are compareTo(Integer obj) and
equals(Object obj).

The compareTo(Integer obj) method compares an Integer object with another and returns an
int value. The return value will be 0 if both the objects contain the same primitive int value. It will return
a positive value if the first object is greater than the second object and a negative value otherwise. The
return value of the method is the numerical difference between the int values in the objects.

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 31 of 68


Session 3
The java.lang Package

The equals(Object obj) compares two Integer objects and returns a boolean value true if the
objects are equal and false otherwise.
Concepts

3.2.5 Float Class

The Float class is associated with the primitive data type float. This class is used to create objects of
numeric type having decimal part. This class contains various methods for conversion and comparison
of Float objects.

The constructor for this class accepts a value of int, float, double or String type. One can create an
object of this class using the following syntax.

Syntax:

Float objFloat1 = new Float(345);


Float objFloat2 = new Float(534.0f);
Float objFloat3 = new Float(“100.56”);

As in other wrapper classes, the Float class also has conversion and comparison methods.

The Float class contains various methods to convert the float type to other types and vice versa. Table
3.5 explains various conversion methods available in the Float class.

Method Name Purpose


intValue() Returns a primitive int value in the Float object
shortValue() Returns a primitive short value in the Float object
longValue() Returns a primitive long value in the Float object
floatValue() Returns a primitive float value in the Float object
doubleValue() Returns a primitive double value in the Float object
toString() Converts the Integer object to a Float value
valueOf(String s) Converts the value represented by the String object to a Float object

Table 3.5: Conversion Methods in the Float Class

Code Snippet 9 displays the integer value of a float number.

Code Snippet 9:

int int1;
Float objFloat1 = new Float(100.45);
int1 = objFloat1.intValue();

page 32 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 3
The java.lang Package

System.out.println(“Integer value is “+int1);

Concepts
Code Snippet 9 will convert the value in the Float object to int and display the value as 100. Please note
that while converting a float value to an int type, the decimal part will be lost.

The comparison methods available in the Float class are compareTo(Float obj) and equals(Object
obj).

The compareTo(Float obj) method compares a Float object with another and returns an int
value. The return value will be 0 if both the objects contain the same primitive float value. It will return a
positive value if the value in the first object is greater than that in the second object and a negative value
otherwise.

The equals(Object obj) compares two Float objects and returns a boolean value true if the
objects are equal and false otherwise.

3.3 Other Classes in the java.lang package

In addition to the wrapper classes, the java.lang package contains classes such as Math, System,
Object, Class, ThreadGroup and Runtime. These classes are useful in creating mathematical
applications, managing classes and objects at runtime and managing system resources.

3.3.1 Math Class

The Math class contains methods to perform basic numeric operations such as rounding off decimal values
to complex mathematical operations such as exponential, logarithmic and trigonometric functions.

The Math class also has two constants Math.E that stores the base value for the natural logarithm and
Math.PI that stores the circumference to diameter ratio of a circle. The Math.E contains a float value
2.718281828459045 and Math.PI contains 3.141592653589793.

At times, it is required to perform various arithmetic and exponential functions in a program. These
functions include calculating the area of a shape, billing, payroll processing and performing statistical
analysis. Table 3.6 explains various arithmetic and exponential methods available in the Math class.

Method Name Purpose


abs() Calculates the absolute value of the int, long, float or double value
passed to it as argument
ceil() Returns the same number if the argument is a natural number or the
nearest natural number greater than the integer part of the argument
otherwise

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 33 of 68


Session 3
The java.lang Package

Method Name Purpose


Concepts

floor() Returns the same number if the argument is a natural number or the
nearest natural number less than the integer part of the argument
otherwise
round(double d) Returns the nearest natural number to the double value given as
argument
max() Returns a double value, which is the greatest of the two numbers
given as arguments. The numbers can be of type int, long, float or
double
min() Returns a double value, which is the least of the two numbers given
as arguments. The numbers can be of type int, long, float or double
pow(double a, double b) Returns the first argument value raised to the power of the second
argument value
sqrt(double d) Returns the square root of the first argument

Table 3.6: Arithmetic and Exponential Methods

The Math class contains a list of logarithmic methods that are used to find the logarithm of a number.
These methods include exp(), log() and log10(). Table 3.7 explains various logarithmic methods in
the Math class.

Method Name Purpose


exp(double d) Returns the value of e to the power of the double value given as argument,
where e is the base value for the natural logarithm
log(double d) Returns the value of natural logarithm, to the base e, of a double value given
as argument

Table 3.7: Logarithmic Methods

The Math class contains a list of trigonometric methods that are used in complex mathematical
and scientific calculations. These methods include sin(double angle), cos(double angle),
tan(double angle), sinh(double angle), cosh(double angle), tanh(double angle),
toDegrees(double angle) and toRadians(double angle). Table 3.8 explains various
trigonometric methods available in the Math class.

Method Name Purpose


sin(double angle) Returns the sine value of an angle
cos(double angle) Returns the cosine value of an angle
tan(double angle) Returns the tangent value of an angle
toDegrees(double angle) Converts the given angle in radians to degrees
toRadians(double angle) Converts the given angle in degrees to radians

Table 3.8: Trigonometric Methods

page 34 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 3
The java.lang Package

Code Snippet 10 shows how to use trigonometric methods.

Concepts
Code Snippet 10:

int rad = 30;


double area = Math.PI * Math.pow(rad,2);
System.out.println(“The area of the circle is “+Math.round(area));

Code Snippet 10 displays 2827, which is the area of a circle having a radius of 30 units. The area value
is also rounded off to the nearest natural number while displaying.

3.3.2 System Class

The System class contains methods to perform standard input - output operations. It also helps in
accessing environment variables and properties defined by the operating system. In addition, this class
provides methods to load files and libraries. This class also contains three constants in, out and err that
represent the standard input, output and error streams respectively. Table 3.9 explains various methods
available in the System class.

Method Name Purpose


exit(int status) Stops the currently running program
gc() Invokes the garbage collector to free up the memory occupied by the
discarded objects
load(String filename) Loads a code file with the specified filename from the local file system
as a dynamic library
setErr (PrintStream err) Sets the specified PrintStream as the standard error output stream
setOut(PrintStream err) Sets the specified PrintStream as the standard output stream
setIn(InputStream err) Sets the specified InputStream as the standard input stream

Table 3.9: Methods of the System Class

3.3.3 Object Class

In the class hierarchy of Java, the Object class is at the root position. All classes, predefined or user-
defined, inherit the methods and properties in the Object class. The Object class contains basic
methods for comparing object, converting one object to another and managing threads. Table 3.10
explains various methods available in the Object class.

Method Name Purpose


equals(Object obj) Returns true if the object that calls this method and the object given as
argument are equal

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 35 of 68


Session 3
The java.lang Package

Method Name Purpose


Concepts

getClass() Returns the runtime class of the object that calls this method
notify() Wakes up a waiting thread
notifyAll() Wakes up all waiting threads
toString() Converts the object to String type
wait() Pauses the currently running thread

Table 3.10: Methods of the Object Class

Code Snippet 11:

Float objFloat = new Float(204.5f);


System.out.println(“String representation of the object flt is: “+objFloat.
toString());

Code Snippet 11 displays the following output:

String representation of the object flt is: 204.5.

page 36 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 3
The java.lang Package

SUMMARY

Concepts
 The java.lang package contains the definitions of basic classes and primitive data types
used in Java.

 Wrapper classes are used in java.lang package to wrap the primitive data types, such as int,
float and double.

 The Math class contains methods to perform various mathematical operations.

 The System class contains methods to perform standard input-output operations, access
environment variables and properties defined by the operating system and to load files and
libraries.

 The Object class contains basic methods for comparing objects, converting one object to
another and managing threads.

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 37 of 68




Learning is a treasure that will
follow its owner everywhere
4
Session

java.io Package

Concepts
Objectives

At the end of this session, you will be able to:

 Explain data streams and their uses.

 Discuss DataInput, DataOutput interface and their methods.

 Discuss InputStream, OutputStream and their subclasses.

 Discuss File class.

 Discuss filter, buffer and character streams.

 Explain serialization.

4.1 Introduction to java.io Package

Software programs work with external data stored locally or across the network. Java works with streams
of data. A physical data storage is mapped to a logical stream and then a Java program reads data from
this stream serially – one byte after another, or character after character. The different types of streams are
byte streams (InputStream, OutputStream) and character streams (Reader and Writer). A physical
file can be read using different types of streams, for example, FileInputStream, or FileReader.

4.1.1 Streams and Uses of Data Streams

A stream can be defined as a flow of data consisting of reader and writer at each end. To read or write
data using Input/Output streams, the following steps need to be performed. They are:

 Open a stream that points at a specific data source: a file, a socket, URL, etc.

 Read or write data from/to this stream.

 Close the stream.

Input and Output streams are abstract classes and are used for reading and writing of unstructured
sequence of bytes. The other input and output streams are subclasses of the basic Input and Output
Stream and are used for reading and writing to a file. The different types of byte streams can be used
interchangeably as they inherit the structure of Input/ Output stream class. For reading or writing bytes, a
subclass of the InputStream or OutputStream class has to be used respectively.

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 39 of 68


Session 4
java.io Package

The read() method reads a byte from the input stream and moves the position of the pointer according
to how many bytes are read. Like the read() method, the write() method will also block until the data
Concepts

is written to an output stream. Most of the methods of the java.io package generates exception and
these stream exceptions are of IOException type which is a subclass of Exception class.

The code in Example 1 uses the FileInputStream class to read a file named “test.txt”. The code prints
each byte’s value separated by white spaces. Byte value returned vary from 0 to 255 and a value of -1,
indicates that the end of the stream has been reached.

Example 1:

FileInputStream testFile = null;


try {
testFile = new FileInputStream(“test.txt”); // open the stream
boolean Eof = false;

while (!Eof) {
int byteVal = testFile.read(); // read the stream
System.out.print(byteVal + “ “);
if(byteVal == -1)
Eof = true;
}

//testFile.close(); // not needed here!!!


} catch (IOException e) {
System.out.println(“Could not read file: “ + e.toString());
} finally {
try{
testFile.close(); // close the stream
} catch (Exception e1){
e1.printStackTrace();
}
}

As seen in the code, the stream is closed in the finally clause of the try/catch block. This is because
if an error occurs while reading the file, the control will automatically be transferred to the catch block.
After the code in the catch block is executed, the code in the finally block will be executed where the
file is closed.

page 40 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 4
java.io Package

4.1.2 More Examples of Stream Applications

Concepts
Reading or writing a single byte or character at a time affects the system performance. Java’s
BufferedInputStream and BufferedOutputStream improves the I/O performance by buffering
the data from the underlying byte streams. For example, a BufferedInputStream objects reads a
number of bytes from an underlying FileInputStream, which in turn reads bytes from a file. Therefore
the application does not have to wait for the next I/O operation to take place to read another byte from the
file. Hence buffered streams make reading and writing data easier and faster.

Let’s modify the example that reads the file “test.txt” to introduce buffering.

Example 2:

import java.io.*;
public class FileIO {
public static void main(String[] args){
FileInputStream testFile = null;
BufferedInputStream buffe =null;
try {
testFile = new FileInputStream(“test.txt”);
buffe = new BufferedInputStream(testFile);
boolean Eof = false;
while (!Eof) {
int byteVal = buffe.read();
System.out.print(byteVal + “ “);
if (byteVal == -1)
Eof = true;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
buffe.close();
testFile.close();
} catch (java.io.IOException e){
}
}
}
}

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 41 of 68


Session 4
java.io Package

4.2 DataInput and DataOutput Interface


Concepts

The DataInput interface reads bytes from a binary stream and reconstructs data from the bytes to any
of the Java primitive type form. Some of the method of the DataInput interface are listed in Table 4.1.

Method Description
float readFloat() The method reads four input bytes and returns a float
value.
void readFully(byte[] byt) The method reads some bytes from an input stream
and stores them into the buffer array b.
void readFully(byte[] byt, int offset, int length) This method reads length number of bytes from an
input stream.
long readLong() This method reads eight input bytes and returns a
long value.
short readShort() This method reads two input bytes and returns a
short value
int readUnsignedByte() This method reads one input byte, zero-extends it to
type int, and returns the result, which is therefore in
the range 0 through 255
int readUnsignedShort() This method reads two input bytes and returns an int
value in the range 0 through 65535.
int skipBytes(int i) The method makes an attempt to skip over i bytes of
data from the input stream, discarding the skipped
bytes.

Table 4.1: Methods of DataInput Interface

The DataOutput interface defines methods that first converts the data from any of the Java primitive
types to a series of bytes and writes those bytes to an underlying binary stream. Some of the methods of
the DataOutput interface are listed in Table 4.2.

Method Description
void write(byte[] byt) The method writes to the output stream all the bytes in
array byt.
void write(byte[] byt, int offset, int The method writes length number of bytes from array b,
length) in order, to the output stream.
void write(int num) The method writes to the output stream the eight low-
order bits of the argument num.
void writeBytes(String str) The method writes the string argument to the output
stream
void writeFloat(float f) The method writes a float value, which is comprised of
four bytes, to the output stream.

page 42 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 4
java.io Package

Method Description

Concepts
void writeLong(long v) The method writes a long value, which is comprised of
eight bytes, to the output stream.
void writeShort(int v) The method writes two bytes to the output stream to
represent the value of the argument.

Table 4.2: Methods of DataOutput Interface

In this session FileInputStream, FileOutputStream, ByteArrayInputStream and


ByteArrayOutputStream class are discussed.

4.3.1 FileInputStream and FileOutputStream Classes

The FileInputStream and FileOutputStream are used to read and write bytes from a file.
File stream objects can be created by either passing the name of the file, or a File object or a
FileDescriptor object respectively. A File object can be created by passing the complete path of
a file along with the file name The File object describes the file and could be a string containing the file
name and an instance of the File class. These classes are used to construct the initial step in a chain
of Stream classes.

The code in Example 3 creates a FileInputStream object to which the filename is passed as an
argument. The object is used to read the text characters from the specified file. The program will print out
its own source code.

Example 3:

import java.io.*;
public class FIStream {
public static void main(String argv[]){
try{
FileInputStream intest = new FileInputStream(“FIStream.java“);
int ch = 0;
while((ch = intest.read())> -1) {
StringBuffer buf = new StringBuffer();
buf.append((char)ch);
System.out.print(buf.toString());
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 43 of 68


Session 4
java.io Package

4.3.2 ByteArrayInputStream and ByteArrayOutputStream


Concepts

A ByteArrayInputStream has an internal buffer which contains bytes that are read from the stream.
It has an internal counter for keeping track of the next byte supplied by the read method.

Using ByteArrayOutputStream class the programmer can write data to a byte array buffer and then
retrieve it by using an application. The internal byte array buffer is dynamically resized to hold all the data
written. Writing large amount of data to this stream causes large amount of memory allocation.

4.4 File Class

Unlike other classes that work on streams, File class directly works with files on the file system. The
files are named using the file-naming conventions of the host operating system. These conventions are
encapsulated using the File class constants. File class encapsulates access to information about a file
or a directory. In other words File class stores the path and name of a directory or file. An instance of the
File class can be used to retrieve information about the attributes of a file, or display the directory listing
or perform basic file system operations. The File class acts as an identifier of files and directories.

The various constructors of the File class can be used to create an instance of the File class. The
following constructor can be used to create a File object.

File fileObj = new File (“Student.txt”);

The string represents the filename in the current directory and is used to construct a File object. The
object referred to by fileObj will be associated with the same file for the complete lifetime of the object.
An instance of the File class can also be created by specifying the filename along with the path as
shown below.

File fileObj = new File(“/tmp/Student.txt”);

The following constructor allows the programmer to separately specify directory path and the file name:

File fileObj = new File(“/tmp”, “Student.txt”);

Some of the methods of the File class are listed in Table 4.3.

Method Description
boolean exists() The method checks whether the file or directory exists.
boolean canWrite() The method checks whether the file can be modified.
boolean canRead() The method checks whether the file referred to by this
File object can be read.
boolean isHidden() The method checks whether the file is a hidden file.

page 44 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 4
java.io Package

Method Description

Concepts
boolean isDirectory() The method checks whether the file is a directory
String getParent() The method retrieves the string representation of the
parent directory.
String getPath () The method converts the pathname to a string
representation.
File[] listFiles() The method retrieves an array of abstract pathname
representing the files present in the directory.
long length() The method returns the length of the file.
long lastModified() The method returns the time when the file was last
modified.
String[] list The method returns the list of file in an array of String
objects
String[] list(FilenameFilter ffObj) The method returns the list of file that satisfy the specified
filter in an array of String objects.

Table 4.3: Methods of File Class

Example 4:

import java.io.*;
class FileFilter implements FilenameFilter {
String ext;
public FileFilter(String ext) {
this.ext = “.” + ext;
}

public boolean accept (File dir, String fName) {


return fName.endsWith(ext);
}
}

public class DirList {


public static void main (String [] args) {
String dirName = “d:/resources”;
File fileObj = new File (“d:/resources”);
FilenameFilter filterObj = new FileFilter(“java”);
String[] fileName = fileObj.list(filterObj);
System.out.println(“Number of files found : “ + fileName.length);

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 45 of 68


Session 4
java.io Package

System.out.println(“” );
Concepts

System.out.println(“Names of the files are : “ );


System.out.println(“------------------------- “ );
for(int ctr=0; ctr < fileName.length; ctr++) {
System.out.println(fileName[ctr]);
}
}
}

The FilenameFilter interface defines an accept() method which to check if the specified file should
be included in a file list. The method of this class returns true if the filename ends with .java extension
as stored in the variable ext. The list() method restricts the visibility of the file and displays only those
files which ends with the specified extension.

4.4.1 FileDescriptor Class

The FileDescriptor class is used to access the file descriptors maintained by operating systems
when files and directories are being accessed. This class does not provide any visibility into the specific
information which is maintained by the operating system. The methods of this class are listed in Table
4.4.

Method Description
void sync() The method determines if a file descriptor object is currently
valid.
boolean valid() The method synchronizes all system buffers with the
underlying storage devices.

Table 4.4: Methods of FileDescriptor Class

4.5 Filter Streams, Buffer Streams and Character Streams

Both the filtered input and output stream classes have the capability to filter I/O in a number of ways. I/O
filters act as a bridge between an input stream and an output stream and perform special processing on
the bytes they transfer from input to output. Filters can be combined to perform many filtering operations
where one filter takes the output of another and works on it.

4.5.1 FilterInputStream and FilterOutputStream

FilterInpuStream is the parent of all filtered input stream classes. The read() method will read from
the underlying stream, filter it and pass the data to another stream. The methods of this class are listed
in Table 4.5.

page 46 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 4
java.io Package

Method Description

Concepts
int available() The method returns the number of bytes read from the input
stream.
void close() The method Closes the input stream and releases the system
resources associated with the stream.
void mark() The method marks a position in the input stream.
boolean markSupported() The method checks whether mark is supported by the input
stream.
int read() The method reads the next byte of data from the underlying
stream.
int read(byte[]b) The method reads b.length bytes of data into an array of bytes
from the underlying input stream.
int read(byte[]b, int offset, int length) The method reads length bytes of data into an array of bytes
starting from the index specified by offset.
void reset() The method repositions the marker to the position at the time
the mark method was invoked.
long skip(long value) The method skips and discards certain bytes of data specified
by value from the input stream.

Table 4.5: Methods of FilterInputStream Class

FilterOutputStream class is the supplement to the FilterInputStream class. It is the parent


class of all filtered output stream classes. The write() method will filter the data and write it to the
underlying stream. Some of the methods of the FilterOutputStream class are displayed in Table
4.6.

Method Description
void close() The method closes the output stream and releases the system
resources associated with the stream.
void flush() The method flushes the output stream and writes the buffered
output bytes to the stream.
void write(byte[] byt) The method writes byt.length bytes of data to the underlying
stream.
void read(int b) The method writes the specified bytes to the underlying stream.
void write(byte[]b, int offset, The method writes length bytes of data into an array of bytes
int length) starting from the index specified by offset.

Table 4.6: Methods of FilterOutputStream Class

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 47 of 68


Session 4
java.io Package

4.5.2 BufferedInputStream and BufferedOutputStream


Concepts

A BufferedInputStream adds the ability to buffer the input. An internal buffer array is created along
with the BufferedInputStream. As bytes are read or skipped, the internal buffer is filled from the
contained input stream.

Java uses buffered input and output to temporarily store data read from or written to a stream. This helps
programs to read or write small amounts of data without adversely affecting the performance of the
system. Filters are present on the buffer and is located between the program and the destination of the
buffered stream. Some of the methods of this class are listed in Table 4.7.

Method Description
int available() The method returns the number of bytes of input that is
available for reading.
void mark(int num) The method places a mark at the current position in the
input stream.
int read() The method reads data from the underlying input stream. It
raises an IOException if an I/O error takes place.
int read(byte [] b, int off, int length) The method reads bytes into the specified byte array from
the given offset. It raises IOException if an I/O error takes
place.
void reset() The method repositions the pointer in the stream to the
point where the mark method was last called.

Table 4.7: Methods of BufferedInputStream Class

BufferedOutputStream performs output buffering in a manner corresponding to the class


BufferedInputStream. BufferedOutputStream stores data in an internal byte array. When the buffer
is full or the stream is flushed, the data is written to an output stream.

It is the same as OutputStream except that it has a flush() method which ensures that the data in the buffer
is written to the actual physical output device. Table 4.7 lists the method of the BufferedOutputStream
class.

Method Description
void flush() The method flushes the buffered output stream.

Table 4.8: BufferedOutputStream Class

4.5.3 Reader and Writer Class

The abstract Reader class is used for reading characters rather than for reading bytes. All the methods
of this class throw an IOException. The read() method returns -1 when end of the file is encountered.

page 48 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 4
java.io Package

Some of the methods of the Reader class are listed in Table 4.9.

Concepts
Method Description
int read() throws IOException The method reads a single character.
int read(char[] buffer, int offset, int count) The method reads character into a section of an
throws IOException array and returns the number of characters that are
read.
int read(char[] buffer) throws IOException The method reads characters into an array and
returns the number of characters read.
long skip(long count) throws IOException The method skips a certain number of characters
as specified by count.
boolean ready() throws IOException The method returns true if the reader is ready to be
read from.
void close() throws IOException The method closes the input stream.

Table 4.9: Methods of the Reader Class

The Writer class is also an abstract class. The methods of the java.io.Writer class are same as the
methods of the java.io.OutputStream class. All the methods of this class throw an IOException in
case of errors. However, they work with characters. Some of the methods of this class are listed in Table
4.10.

Method Description
void write(int c) throws IOException The method writes a single character.
void write(char[] text) throws IOException The method writes a complete array of characters
to an output stream.
void write(char[] text, int offset, int length) The method writes a portion of an array of characters
to an output stream starting from offset. The variable
length specifies the number of characters to write.
void write(String s) throws IOException The method writes a string to the output stream.
void write(String s, int offset, int length) throws The method writes a portion of a string to the output
IOException stream starting from offset. The variable length
specifies the number of characters to write.
void flush() throws IOException The method flushes the output stream so that
buffers are cleared.
void close() throws IOException The method closes the output stream.

Table 4.10: Methods of the Writer Class

4.5.4 PrintWriter Class

The PrintWriter class represents formatted information of objects to the text-output stream. It

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 49 of 68


Session 4
java.io Package

implements all the print methods of the PrintStream class. It does not have methods for writing raw
bytes. In such a case, a program uses unencoded byte streams. The PrintWriter class differs from
Concepts

the PrintStream class as it can handle multiple bytes and other character sets properly.

The main advantage of the print() and println() method is that any Java object or literal or variable
can be printed by passing it as an argument. If the autoFlush option is set to true then automatic flushing
takes place when println() method is invoked. The println() method follows its argument with
a platform dependent line separator. The print() method does not flush the stream automatically.
Otherwise, both these methods are same. Some of the methods of this class are listed in Table 4.11.

Method Description
boolean checkError() The method flushes the stream if it’s open and check the error
state.
void print(boolean b) The method prints a boolean value.
void print(char c) The method is used to print a character.
void print(char[] s) The method is used to print an array of characters
void print(double d) The method prints a double-precision floating-point number.
void print(float f) The method prints a floating-point number.
void print(int i) The method is used to print an integer.
void print(long l) The method is used to print a long integer.
void print(Object obj) The method prints an object.
void print(String s) The method prints a string.
void println() The method terminates the current line by using the line separator
string.
void println(boolean x) The method prints a boolean value and then terminate the line.
void flush() throws IOException The method flushes the output stream so that buffers are
cleared.
void close() throws IOException The method closes the output stream.
void setError() The method indicates that an error has occurred.
void write(char[] buf) The method is used for writing an array of characters.
void write(char[] buf, int off, int The method is used for writing a part of an array of characters.
len)
void write(int c) The method writes a single character.
void write(String s) The method writes a string.
void write(String s, int off, int len The method writes a part of a string.

Table 4.11: Methods of PrintWriter Class

The PrintWriter class implements and overrides the abstract write() method from the Writer
class. The only difference is that none of the write() methods of the PrintWriter class throws an
IOException because it is caught inside the class and an error flag is set.

page 50 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 4
java.io Package

4.5.5 CharacterArrayReader and CharacterArrayWriter

Concepts
The CharacterArrayReader class uses a character array as the underlying source of text to be read.
This class does not have an underlying input stream but has an underlying array of characters. Some of
the methods of this class are listed in Table 4.12.

Method Description
long skip(long n) The method skips n number of characters before reading.
void mark(int num) The method marks the current position in the stream.
int read() The method reads a single character.
int read(char[] b, int off, int length) The method reads characters into the specified character array
from the given offset.
void reset() The method repositions the pointer in the stream to the point
where the mark method was last called or to the beginning of the
stream if it has not been marked.
boolean ready() The method is used to confirm whether this stream is ready to
be read.
void close() The method closes the input stream.

Table 4.12: Methods of CharArrayReader Class

The CharArrayWriter class provides a dynamically growing character array to hold the output. Some
of the methods of this class are listed in Table 4.13.

Method Description
void close() The method closes the stream.
void flush() The method flushes stream.
void write() The method writes a single character to the array.
void write(char[] b, int off, int length) The method writes characters to the buffer.
void reset() The method repositions the pointer in the buffer to the point
where the mark method was last called or to the beginning
of the buffer if it has not been marked.
int size() The method returns current size of the buffer.
char[] toCharArray() The method returns a copy of the data.
String toString() The method is used to convert the input data to string.
void write(String str, int off, int len) The method writes a portion of string to buffer.
void writeTo(Writer out) The method is used to write the contents of buffer to a
character stream.

Table 4.13: Methods of CharArrayWriter Class

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 51 of 68


Session 4
java.io Package

Example 5 demonstrates the use of CharArrayWriter class.


Concepts

Example 5:

import java.io.*;

public class Program {


public static void main(String[] args) throws IOException {

// Create a CharArrayWriter object which can hold 11 characters.


CharArrayWriter writer = new CharArrayWriter(11);

String str =”Hello Aptech”;


writer.write(“Hello Aptech”, 6, str.length() - 6);

System.out.println(“The CharArrayWriter buffer contains: “ + writer.


toString());
writer.flush();

// Print out the contents of the CharArrayWriter buffer.


System.out.println(“After flushing the CharArrayWriter buffer “ + “contains:
“ + writer.toCharArray());

// Now reset the buffer we just populated.


writer.reset();

// Print out the contents of the CharArrayWriter buffer.


System.out.println(“After reset CharArrayWriter buffer “ + “contains: “
+ writer.toCharArray());

// Close the CharArrayWriter and StringWriter buffers.


writer.close();
}
}

The code initializes a string variable and copies a portion of the string to an instance of CharArrayWriter
class and displays the content of the CharArrayWriter object.

page 52 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 4
java.io Package

4.6 Serialization

Concepts
Object serialization saves the state of an object as sequence of bytes and at latter time the object is
reconstituted from this bytes. Serialization was developed so that it can be used in RMI. Objects are
serialized by object output stream and deserialized by object input stream.

4.6.1 ObjectInputStream Class

An ObjectInputStream deserialises objects previously written using an ObjectOutputStream.


ObjectOutputStream and ObjectInputStream can provide an application with persistent
storage objects when used with a FileOutputStream and FileInputStream respectively.
ObjectInputStream is used to retrieve previously serialised objects. Some of the methods of the
ObjectInputStream class are listed in Table 4.14.

Method Description
int available() The method returns the number of bytes read without
blocking.
void close() The method closes the stream.
void defaultReadObject() The method reads non-static and non-transient fields of
current class from this stream.
int read() The method reads one byte of data.
int read(byte[], int, int) The method reads into an array of bytes.
boolean readBoolean() The method reads boolean value.
byte readByte() The method reads a byte
char readChar() The method reads a 16 bit char.
double readDouble() The method reads a 64 bit double.
int readInt() The method reads a 32 bit int.
long readLong() The method reads a 64 bit long.
short readShort() The method reads a 16 bit short.
Object readObject The method reads an object from the object input stream.

Table 4.14: Methods of ObjectInputStream Class

The following program demonstrates how to use an ObjectInputStream class and deserialises an
object. The object had already been created using the ObjectOutputStream class. The following
steps have been followed in the program:

1. Opens an input stream.

2. Chains it with the ObjectInputStream.

3. Calls the method readObject() and cast the returned object to the class that is being

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 53 of 68


Session 4
java.io Package

deserialised.
Concepts

4. Closes the streams.

Example 6:

import java.io.*;

class Employee1 implements java.io.Serializable {


String lastName;
String firstName;
double sal;
java.util.Date hireDate;
String addr;
}

public class BranchEmpProcessor {


public static void main(String[] args) {
FileInputStream fIn=null;
ObjectInputStream oIn=null;
try{
fIn= new FileInputStream(“c:\\NewEmployee.ser”);
oIn = new ObjectInputStream(fIn);

//de-serializing employee
Employee1 emp = (Employee1) oIn.readObject();
System.out.println(“Deserialized “ + emp.firstName + “ “ + emp.
lastName + “ from NewEmployee.ser “);

} catch(IOException e) {
e.printStackTrace();
} catch(ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
oIn.close();
fIn.close();
} catch (IOException e1) {

page 54 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 4
java.io Package

e1.printStackTrace();

Concepts
}
}
}
}

4.6.2 ObjectOutputStream Class

The ObjectOutputStream class serializes Java objects. It extends the functions of DataOutputStream
to objects and includes arrays, Strings, and any class of Java object. The default method for serializing
objects is effective in most cases but in some specific cases, objects may need to control the way they are
serialised. ObjectOutputStream calls a private method of the object and controls its own serialized
form.

Some of the methods of the ObjectOutputStream class are listed in Table 4.15.

Method Description
void annotateClass(Class c) The method allows class data to be stored in the stream.
void close() The method closes the stream.
void defaultWriteObject() The method writes the non-static and non-transient fields of
the current class to this stream.
void drain() The method drains any buffered data to
ObjectOutputStream.
void flush() The method flushes the stream.
Object replaceObject(Object obj) The method allows subclasses of ObjectOutputStream to
substitute one object for another during serialization.
void reset() The method resets and disregards the state of any objects
already written to the stream.
void write(byte[] b) The method writes an array of bytes.
void write(byte[] b, int offset, int len) The method writes a sub array of bytes starting from offset
and is of len bytes long.
void write(int num) The method writes a byte.
void writeBoolean(boolean b) The method writes a boolean value.
void writeByte(int num) The method writes an 8 bit byte.
void writeBytes(String str) The method writes a String as a sequence of bytes.
void writeChar(int ch) The method writes a 16 bit char.
void writeChars(String str) The method writes a String as a sequence of characters.
void writeDouble(double val) The method writes a 64 bit double.
void writeInt(int num) The method writes a 32 bit int.
void writeLong(long val) The method writes a 64 bit long.

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 55 of 68


Session 4
java.io Package

Method Description
Concepts

void writeShort(int num) The method writes a 16 bit short.


void writeStreamHeader() The method allows subclasses to append or prepend their
own header to the stream
void writeUTF(String str) The method writes a String in UTF format.

Table 4.15: Methods of ObjectOutputStream Class

The following program uses the ObjectOutputStream class and serialises an object into a stream to
perform the following actions:

1. Open one of the output streams, for example FileOutputStream

2. Chain it with the ObjectOutputStream

3. Call the method writeObject() providing the instance of a Serializable object as an argument

4. Closes the streams

Example 7:

import java.io.*;
import java.util.Date;

class Employee1 implements java.io.Serializable {


String lastName;
String firstName;
double sal;
java.util.Date hireDate;
String addr;
}

public class HeadQuarterEmpProcessor {


public static void main(String[] args) {
Employee1 emp = new Employee1();
emp.lastName = “Varma”;
emp.firstName = “Ravi”;
emp.sal = 5050;

page 56 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 4
java.io Package

emp.addr = “12 Russel Street”;

Concepts
emp.hireDate = new Date();
FileOutputStream fOut1=null;
ObjectOutputStream oOut1=null;
try{
fOut1= new FileOutputStream(“c:\\NewEmployee.ser”);
oOut1 = new ObjectOutputStream(fOut1);
oOut1.writeObject(emp); //serializing employee
System.out.println(“An employee is serialized” + “into c:\\
NewEmployee.ser”);
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
oOut1.flush();
oOut1.close();
fOut1.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 57 of 68


Session 4
java.io Package

SUMMARY
Concepts

 The DataInputInterface and DataOutputInterface defines methods for reading primitive data
types and for converting data from any of the Java primitive types.

 The FileInputStream and FileOutputStream use a kind of File as a constructor.

 Character streams allow a way to handle character based input/output operations.

 File descriptor is used to create a FileInputStream or FileOutputStream to contain it.

 The CharArrayReader and CharArrayWriter classes are same as ByteArrayInputStream and


ByteArrayOutputStream as they support input and output from memory buffers.

 Serialization is the process of reading and writing objects to a byte stream.

page 58 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


5
Session

java.util and
Collections API

Concepts
Objectives

At the end of this session, you will be able to:

 Describe the java.util package

 Describe the Collections Framework

 Explain the various collection classes and interfaces

5.1 java.util Package

The java.util package defines a number of useful classes, primarily, collection classes that are useful
for working with groups of objects. This package is not a utility package that is part of the Java language,
instead Java depends directly on several of the classes in this package.

The classes in this package such as Date and Calendar are explained in the following sections.

 Date Class, its Constructors and Methods

The Date class object represents date and time and provides methods for manipulating the date
and time instances. Date is represented as a long type that counts the number of milliseconds since
January 1, 1970, 00:00:00 GMT. A Date object cannot be printed without converting it to a String
type. The format should conform to the end-user’s locale, for example, 12.2.95 or 02/12/95.

Constructors of Date class are listed in the Table 5.1.

Constructor Description
Date() The constructor creates a Date object using today’s date.
Date(long dt) The constructor creates a Date object using the specified number of milliseconds
since January 1, 1970, 00:00:00 GMT.

Table 5.1: Constructors of the Date Class

 Calendar Class, its Constructors and Methods

Based on a given Date object, the Calendar class can retrieve information in the form of integers
such as YEAR, MONTH and DAY. It is an abstract class and hence cannot be instantiated like the
Date class.

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 59 of 68


Session 5
java.util and Collections API

A Calendar object provides all the necessary time field values needed to imple-
ment the date-time formatting for a particular language and calendar style (for example,
Concepts

German-Gregorian, German-Traditional).

Note: GregorianCalendar is a subclass of Calendar that implements the Gregorian form of


a calendar.

 Random Class

The Random class is used to generate random numbers. It is used whenever there is a need to gen-
erate numbers in an arbitrary or unsystematic fashion. For instance, in a dice game, the outcome of
the dice throw is unpredictable. The game can be simulated using a Random object.

Two constructors are provided for this class, one taking a seed value (a seed is a number that is
used to begin random number generation) as a parameter and the other taking no parameters and
using the current time as a seed.

5.2 Collections Framework

A collection is a group of elements. In Java, there can be a collection of objects of interfaces and classes.
To manipulate and work efficiently with such collections as a whole, Java provides the Collections
framework. The Collections framework is based on a set of interfaces. Some of the interfaces supported
by the java.util package are:

 Collection

 List

 Map

 Set

 SortedMap

 SortedSet

Some of the collection classes that implements the interfaces in the java.util package are:

 ArrayList

 HashSet

 HashMap

page 60 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 5
java.util and Collections API

 LinkedList

Concepts
 TreeMap

 TreeSet

The Collection interface supports add() and remove() methods to insert and remove elements.
Some of the other important methods supported by the Collection interface are listed in Table 5.2.

Method Description
clear() The method clears or removes all the contents from the collection
isEmpty() The method returns true if the collection contains no elements
size() The method returns the number of elements in this collection
toArray() The method returns an array containing all the elements of this collection

Table 5.2: Methods Supported by Collection Interface

5.3 List Interface

In programming terminology, a list is an ordered collection of elements. The user can have control of
the order in which a given element has to be inserted. A user can access a particular element or search
for elements in the list. Lists allow duplication of elements and also null elements. Some of the methods
supported by the List interface are listed in Table 5.3.

Method Description
indexOf(Object o) The method returns the index of the first occurrence of the
specified element in the list, or returns -1 in case the list does not
contain the given element.
subList(int fromIndex, int toIndex) The method returns elements of the list between the specified
fromIndex and toIndex. The returned list contains elements
starting at fromIndex upto toIndex excluding the element specified
at toIndex.
lastIndexOf(Object o) The method returns the index of the last occurrence of the specified
element in the list, or returns -1 in case the list does not contain
the given element.
get(int index) The method returns the element at the specified position.

Table 5.3: Methods of List Interface

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 61 of 68


Session 5
java.util and Collections API

5.3.1 ArrayList Class


Concepts

In Java, when an array is created, the size of the array is specified at the time of declaration of the array.
They are fixed length array and cannot grow or shrink in size. Java provides support to dynamic array that
can grow as required in the form of the ArrayList class. This class is used in cases where the number
of elements being stored in the array and the size is not known in advance. An instance of ArrayList
class can be created with an initial size. Later, as and when elements are added, the size increases and
the array expands.

The ArrayList class includes all elements, including null. In addition to implementing the methods of
the List interface, this class provides methods to change the size of the array that is used internally to
store the list.

Some of the important methods in the ArrayList class are listed in Table 5.4.

Method Description
ensureCapacity(int minCapacity) The method increases the capacity of the specified ArrayList
such that it can hold atleast the number of elements
specified in minCapacity.
removeRange(int fromIndex, int toIndex) The method removes all the elements within the specified
range excluding the element specified at toIndex but
including the element specified at fromIndex.
clone() The method returns a shallow copy of the Arraylist object.

Table 5.4: Methods of ArrayList Class

5.3.2 LinkedList

A linked list is an important data structure. It consists of a series of nodes. Each node contains data as
well as one or two links pointing to the next and/or previous nodes.

Linked lists allow insertion and removal of nodes at any position in the list, but do not allow random access.
There are several different types of linked lists - singly-linked lists, doubly-linked lists, and circularly-linked
lists.

Java provides the LinkedList class in the java.util package to implement linked lists.

Some of the important methods in this class are listed in Table 5.5.

Method Description
addFirst(Object o) The method inserts the specified element at the
beginning of the list.

page 62 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 5
java.util and Collections API

Method Description

Concepts
addLast(Object o) The method inserts the specified element at the
end of the list.
removeFirst() The method removes and returns the first element
of the list.
removeLast() The method removes and returns the last element
of the list.

Table 5.5: Methods of LinkedList Class

5.4 Set Classes and Interfaces

A Set is a collection of elements that does not contain any duplicate elements.

5.4.1 Set Interface

The Set interface contains methods inherited from Collection interface with the restriction that
duplicate elements are prohibited.

The Java platform contains three general-purpose Set implementations. They are:

 HashSet

HashSet, stores its elements in a Hashtable, and does not guarantee the order of iteration.

 TreeSet

TreeSet, stores its elements in a tree, and orders its elements based on their values.

 LinkedHashSet

LinkedHashSet, implements Hashtable and a linked list implementation of the Set interface. It
has a doubly linked list running through all its elements and thus is different from HashSet. Linked
list iterates through the elements in the orders in which they were inserted into the set (insertion-
order).

Comparison of Set with List

The Set interface is an extension of the Collection interface and defines a set of elements. The
difference between List and Set is that the Set does not permit duplication of elements. Set is
used to create non-duplicate list of object references. Therefore, add() method returns false if duplicate
elements are added.

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 63 of 68


Session 5
java.util and Collections API

5.4.2 SortedSet Interface


Concepts

The SortedSet interface extends the Set interface. It maintains the elements in a sorted manner. It
represents collections consisting of unique, sorted elements. The class TreeSet is an implementation
of interface SortedSet.

5.4.3 TreeSet

The elements are ordered using their natural ordering, or using a Comparator provided at set creation
time, depending on which constructor is used.

5.4.4 LinkedHashSet Class

The LinkedHashSet is used in the same way as the HashSet class. It has no additional methods of
its own and has constructors similar to the HashSet. The privilege of using LinkedHashSet is that
it maintains the order of the items added to the Set. It does this by maintaining a doubly linked list
containing the hash and the original order of the items.

5.5 Map Classes and Interfaces

A map maps keys to values. A map cannot contain duplicate keys. Each key in a map can map to only
one value.

5.5.1 Map Interface

The Map interface allows storage of elements in pairs, termed “keys” and “values”, where each key
maps to one value. For a better understanding, lists which have numeric keys can be considered as
Map objects. However, there is no direct connection between a List and Map except that they are both
defined in java.util package. The Map interface does not extend the Collection interface.

Maps have their own hierarchy, for maintaining the key-value associations. The interface describes a
mapping from keys to values, without duplicate keys.

Some of the methods supported by this interface are listed in Table 5.6.

Method Description
hashCode() Returns the hashCode value for this map
values() Returns the values contained in the map
size() Returns the number of key-value mappings in
this map

page 64 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 5
java.util and Collections API

Method Description

Concepts
remove(Object key) Removes the mapping for the specified key, if
present

Table 5.6: Methods of Map Interface

5.5.2 HashMap Class

The HashMap is very similar to the Hashtable with two main differences:

 The HashMap is not synchronized making access faster

 The HashMap allows null values to be used as values or keys, which are disallowed in the
Hashtable implementation. HashMap class does not guarantee the order of the map and it does
not guarantee that the order will remain constant over time.

5.5.3 TreeMap Class

The TreeMap class implements the Map interface but stores elements in a tree. The TreeMap returns
keys in sorted order. If there is no need to retrieve Map elements sorted by key, then the HashMap would
be a more practical structure to use.

5.5.4 LinkedHashMap Class

A LinkedHashMap is a combination of Hashtable and linked list. The order of the output is predictable
since the iteration order is determined by the insertion order. So one can get the key/values back in the
order that they were added to this Map.

5.6 Arrays Class

An array is a data structure consisting of a numbered list of items, where all the items are of the same
type. The items in an array are numbered from zero up to some maximum value, which is set when the
array is created. For example, an array might contain 100 integers, numbered from 0 to 99.

The class java.util.Arrays provides a number of static methods for filling, searching, sorting, and
comparing arrays. The important methods of the Arrays class are listed in Table 5.7.

Method Description
equals(char[] a1, char[] a2) Returns ‘true’ if the two arrays containing elements
of type character are equal (containing same
elements in the same order)

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 65 of 68


Session 5
java.util and Collections API

Method Description
Concepts

fill(boolean[ ] a, boolean value) Assigns the boolean “value” to each element of the
specified array “a” of boolean values
sort(byte[ ] a) Sorts the specified array of bytes in ascending
order
binarySearch(byte[ ] a, byte key) Searches for the specified key in the given array

Table 5.7: Methods of Arrays Class

page 66 of 68 Version 1.0 © 2013 Aptech Limited A Guide to Advanced Java


Session 5
java.util and Collections API

SUMMARY

Concepts
 Collection interface represents a collection of objects.

 Collections class contains methods that work on the Collection of objects.

 ArrayList is an array representation of the List interface.

 Vector is an array of objects whose size can be increased.

 List is an ordered collection of elements.

 Map is an object that maps keys to values.

 SortedMap is a map in which the elements are maintained in the ascending order of the
keys in it.

 Set is a collection of elements that does not contain duplicates.

 HashSet is a Set interface implemented by a Hashtable.

 LinkedHashSet is a Set interface implemented by a combination of Hashtable and


LinkedList. The return order of the elements is predictable.

 TreeSet is a Set interface backed by a TreeMap instance.

 HashMap is Hashtable based implementation of a Map interface.

 LinkedHashMap is a Map interface implemented by a combination of Hashtable and


LinkedList. The return order of the elements is predictable.

 TreeMap is a tree based implementation of a SortedMap.

A Guide to Advanced Java Version 1.0 © 2013 Aptech Limited page 67 of 68


“ “
I am always ready to learn although
I do not always like being taught
Reader’s Response
Name Of Book : _______________________________________________________________________

Batch :______________________________________________ Date : __________________________

The members of the design team at Aptech Worldwide are always striving to enhance the quality of the
books produced by them. As a reader, your suggestions and feedback are very important to us. They are of
tremendous help to us in continually improving the quality of this book. Please rate this book in terms of the
following aspects.

Aspects Rating

Excellent Very Good Good Poor

Presentation style

Suggestion :

_____________________________________________________________________________________

_____________________________________________________________________________________

Simplicity of language

Suggestion :

_____________________________________________________________________________________

_____________________________________________________________________________________

Topics chosen

Suggestion :

_____________________________________________________________________________________

_____________________________________________________________________________________

Topic coverage

Suggestion :

_____________________________________________________________________________________

_____________________________________________________________________________________
Aspects Rating

Excellent Very Good Good Poor

Explanation provided

Suggestion :

__________________________________________________________________________________

__________________________________________________________________________________

Quality of picture / diagrams

Suggestion :

__________________________________________________________________________________

__________________________________________________________________________________

Overall suggestions :

__________________________________________________________________________________

__________________________________________________________________________________

__________________________________________________________________________________

__________________________________________________________________________________

Please fill up this response card and send it to :

The Design Centre,

APTECH LIMITED
Aptech House,
A-65, MIDC,
Andheri (East),
Mumbai - 400 093,
INDIA.

Your efforts in this direction will be most appreciated

You might also like