SlideShare a Scribd company logo
Introduction to Java
The Java architecture consists of:
• a high-level object-oriented programming language,
• a platform-independent representation of a compiled class,
• a pre-defined set of run-time libraries,
• a virtual machine.
This book is mainly concerned with the language aspects of Java and the associated
java.lang library package. Consequently, the remainder of this section provides a
brief introduction to the language. Issues associated with the other components will be
introduced as and when needed in the relevant chapters.
The introduction is broken down into the following components
• identifiers and primitive data types
• structured data types
• reference types
• blocks and exception handling
• control structures
• procedures and functions
• object oriented programming, packages and classes
• inheritance
• interfaces
• inner classes.
1 Identifiers and primitive data types
Identifiers Java does not restrict the lengths of identifiers. Although the language does allow the
use of a “_” to be included in identifier names, the emerging style is to use a mixture of
upper and lower case characters. The following are example identifiers:
http://professional-guru.com
2 Introduction to Java
Note that Java is case sensitive. Hence, the identifier exampleNameInJava is differ-
ent from ExampleNameInJava.
Primitive
data types
Java provides both a variety of discrete data types and a floating point type.
Discrete data types. The following discrete data types are supported:
• int — a 32-bit signed integer;
• short — a 16-bit signed integer;
• long — a 64-bit signed integer;
• byte — an 8-bit signed integer;
• boolean — the truth values, true and false;
• char — Unicode characters (16 bits).
All the usual operators for these types are available.
Enumeration types have been introduces as of Java 1.5. Although not a primitive
type it behaves like a descrete type and can be used in a switch statement. The follow-
ing example illustrates a simple enumeration type for days of the week.
Floating
point
numbers
Java provides float and double types that support the 32 and 64 bit IEEE 754 float-
ing point values respectively. Note, that floating point literals are automatically consid-
ered to be of double precision. Consequently, they must either be explicitly converted
to float or they can be followed by the letter f. For example:
exampleNameInJava
example_name_in_Java
public enum Day{SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY}
float bodyTemperature = (float) 98.6;
// or
float bodyTemperature = 98.6f;
http://professional-guru.com
Introduction to Java 3
Note that the assignment operator is “=” and that comments are delimited by “/*” and
“*/”. Java also allows “//” for comments ending a line.
2 Structured data types
Java supports arrays; both single and multi dimensional arrays can be created; for
example:
Note that arrays are represented by objects and that indexes start at 0. The length of the
array can be determined by the length field associated with the array
Java has no record structure as such. However, the same effect can be achieved
using classes. The details of classes will be given in Section 7 but, for now, consider the
following class for the date:
It is not possible to express range constraints on the values of the date's components.
Note also that as a “record” is a class, an allocator (the new operator) must be used to
create an instance of the Date. Initialization of the object can be achieved using con-
structors (see Section 7).
Important
note
final int max = 10; // a constant
float[] reading = new float[max]; // index is 0 .. max-1
boolean[][] switches = new boolean[max][max];
class Date {
int day, month, year;
}
Date birthDate = new Date();
birthDate.day = 31;
birthDate.month = 1;
birthDate.year = 2000;
All objects created by the new operator are stored in an area of memory called the
heap. An activity called garbage collection will free up any associated memory
when the object is no longer referenced.
http://professional-guru.com
4 Introduction to Java
3 Reference types
All objects in Java are references to the actual locations containing the encapsulated
data, hence no additional access or pointer type is required. Furthermore, no forward
declaration is required. For example:
Note that as a result of representing objects as reference values, comparing two objects
is a comparison between their references not their values. Hence,
will compare the locations of the objects not the values encapsulated by the objects (in
this case, the values of the value and next fields). To compare values requires a
class to implement an explicit equals method. A similar situation occurs with object
assignment. The assignment operator assigns to the reference. To create an actual copy
of an object requires the class to provide methods to clone the object.
4 Blocks and exception handling
In Java, a block (or compound statement) is delimited by “{“ and “}”, and usually has
the following structure
class Node {
int value;
Node next;
// The type node can be used even though
// its declaration has not been completed yet.
}
Node ref1 = new Node();
Node ref2 = new Node();
. . .
if(ref1 == ref2) { ... }
http://professional-guru.com
Introduction to Java 5
although the declarative part can be dispersed throughout the block.
Java can have exception handlers at the end of a block if the block is labeled as a
try block. Each handler is specified using a catch statement. Consider, the following:
The catch statement is like a function declaration, the parameter of which identifies the
exception type to be caught. Inside the handler, the object name behaves like a local
variable. A handler with parameter type T will catch a thrown object of type E if:
• T and E are the same type,
• or T is a parent (super) class of E at the throw point.
It is this last point that integrates the exception handling facility with the object-ori-
ented programming model. In the above example the Exception class is the root
class of all application exceptions. Hence, the catch clause will catch all application
exceptions. Here, getMessage is a method declared in the Exception class. A call
to E.getMessage will execute the appropriate routine for the type of object thrown.
If no exception handler is found in the calling context of a function, the calling
context is terminated and a handler is sought in its calling context. Hence, Java supports
exception propagation.
Finally
clauses
Java also supports a finally clause as part of a try statement. The code attached to
this clause is guaranteed to execute whatever happens in the try statement irrespective
{
< declarative part >
< sequence of statements >
}
try {
//code that might throw an exception
} catch (Exception err) {
// Exception caught, print error message on
// the standard output.
System.out.println(err.getMessage());
}
http://professional-guru.com
6 Introduction to Java
of whether exceptions are thrown, caught, propagated or, indeed, even if there are no
exceptions thrown at all.
Checked and
unchecked
exceptions
In Java, all exceptions are subclasses of the predefined class java.lang.Throw-
able. The language also defines other classes, for example: Error, Exception,
and RuntimeException. The relationship between these (and some common
exceptions) is depicted in Figure 1.
FIGURE 1. Part of the The Java Predefined Throwable Class Hierarchy
Throughout this book, the term Java exception is used to denote any class derived from
Exception. Objects derived from Error describe internal errors and resource
exhaustion in the Java run-time support system. Although these errors clearly have a
major impact on the program, there is little that the program can do when they are
try {
...
} catch(...) {
...
} finally {
// code that will be executed under all circumstances
}
Throwable
Error Exception
RunTimeException
IllegalArgumentException
IllegalThreadStateException
SecurityException
InterruptedException
NullPointerException
checked
unchecked
IllegalMonitorStateException
http://professional-guru.com
Introduction to Java 7
thrown (raised) as no assumptions can be made concerning the integrity of the system.
Objects derived from the Exception hierarchy represent errors that programs can
handle or can throw themselves. RuntimeExceptions are those exceptions that are
raised by the run-time system as a result of a program error. They include errors such as
those resulting from a bad cast (ClassCastException), array bounds error
(IndexOutOfBoundException), a null pointer access (NullPointerExcep-
tion), integer divide by zero (ArithmeticException) etc.
Throwable objects which are derived from Error or RuntimeExceptions
are called unchecked exceptions, the others are termed checked exceptions. Checked
exceptions must be declared by the function that can throw them. The compiler will
check that an appropriate handler can be found. The compiler makes no attempt to
ensure that handlers for unchecked exceptions exist.
5 Control structures
Control structures can be grouped together into three categories: sequences, decisions
and loops.
Sequence
structures
Most languages implicitly require sequential execution, and no specific control struc-
ture is provided. The definitions of a block in Java indicate that between { and } there
is a sequence of statements. Execution is required to follow this sequence.
Decision
structures
The most common form of decision structure is the if statement; for example:
In general, a multiway decision can be more explicitly stated and efficiently imple-
mented, using a switch structure.
if(A != 0) {
if(B/A > 10) {
high = 1;
} else {
high = 0;
}
}
http://professional-guru.com
8 Introduction to Java
As with C and C++, it is necessary to insert statements to break out of the switch once
the required command has been identified. Without these, control continues to the next
option (as with the case of A).
Loop
(iteration)
structures
Java supports the usual for and while statements. The following example illustrates
the for statement; the code assigns into the first ten elements of array, A, the value of
their positions in the array:
The free use of the loop control variable (i here) in this manner can be the cause of
many errors, consequently, Java also allows a local variable to be declared inside the
for loop.
An example while statement is given below:
switch(command) {
case 'A' :
case 'a' : action1(); break; /* A or a */
case 't' : action2(); break;
case 'e' : action3(); break;
case 'x' :
case 'y' :
case 'z' : action4(); break; /* x, y or z */
default : /* no action */
}
for(i = 0; i <= 9; i++) {
/* i must be previously declared */
A[i]= i; /* i can be read/written in the loop */
} /* the value of i is defined */
/* after the loop */
for(int i = 0; i <= max; i++) {
A[i]= i;
}
http://professional-guru.com
Introduction to Java 9
Java also supports a variant where the test occurs at the end of the loop:
The flexibility of an iteration statement is increased by allowing control to pass out of
the loop (that is, the loop to terminate) from any point within it (break) or for control
to pass directly to the next iteration (continue):
6 Procedures and functions
Procedures and functions can only be declared in the context of a class, they are known
collectively as methods. In fact, strictly Java only supports functions; a procedure is
considered to be a function with no return value (indicated by void in the function
definition).
Java passes primitive data type (int, boolean, float etc.) parameters (often called
arguments) by value. Variables of class type are reference variables. Hence, when they
are passed as arguments they are copied, but the effect is as if the object was passed by
reference. Consider a function that calculates the roots of a quadratic equation:
while(<expression>) {
/* Expression evaluating to true or false. */
/* False implies loop termination. */
<sequence of statements>
}
do {
< sequence of statements>
} while (<expression>);
while(true) {
...
if(<expression1>) break;
if(<expression2>) continue;
...
}
http://professional-guru.com
10 Introduction to Java
Note Java requires the roots of the equation to be passed as a class type, as primitive
types (including double) are passed by copy and there are no pointer types.
Function
bodies
The bodies of functions are illustrated by completing the quadratic definitions
given above.
The invoking of a function merely involves naming the function (including any object
or class name) and giving the appropriately typed parameters in parentheses.
7 Object-oriented programming, packages and
classes
Objects have four important properties [Wegner, 1987]. They have
• inheritance (type extensibility)
public class Roots {
double R1, R2;
}
// the following must be declared in a class context
boolean quadratic(double A, double B, double C,
Roots R);
boolean quadratic(double A, double B,
double C, Roots R) {
double disc;
disc = B*B - 4.0*A*C;
if(disc < 0.0 || A == 0.0) { // no roots
R.R1 = 0; // arbitrary values
R.R2 = 0;
return false;
}
R.R1 = (-B + Math.sqrt(disc)) / (2.0*A);
R.R2 = (-B - Math.sqrt(disc)) / (2.0*A);
return true;
}
http://professional-guru.com
Introduction to Java 11
• automatic object initialization (constructors)
• automatic object finalization (destructors)
• dynamic binding of method calls (polymorphism — sometimes called run-time
dispatching of operations).
All of which are supported, in some form, by Java’s class and interface mechanisms.
Related classes and interfaces can be grouped together into larger units called pack-
ages.
Inheritance is perhaps the most significant concept in an object abstraction. This
enables a type (class) to be defined as an extension of a previously defined type. The
new type inherits the “base” type but may include new fields and new operations. Once
the type has been extended then run-time dispatching of operations is required to
ensure the appropriate operation is called for a particular instance of the family of
types.
Earlier in this section, a simple class for the date type was introduced.
This example only illustrates how data items can be grouped together. The full facilities
of a class allow the data items (or instance variables or fields as Java calls them) to be
encapsulated and, hence, abstract data types to be defined. As an example, consider the
class for a queue abstraction.
First, a package to contain the queue can be declared (if there is no named pack-
age, then the system assumes an unnamed package). Items from other packages can be
imported. Then classes to be declared inside the package are given. One of these
classes Queue is declared as public, and it is only this that can be accessed outside
the package. The keyword public is termed a modifier in Java, the other ones for
classes include abstract and final. An abstract class is one from which no
objects can be created. Hence, the class has to be extended (to produce a subclass) and
that subclass made non-abstract before objects can exist. A final modifier indicates
that the class cannot be subclassed. No modifier indicates that the class is only accessi-
ble within the package. A class can have more than one modifier but certain combina-
tions, such as abstract and final, are meaningless.
Each class can declare local instance variables (or fields), constructor methods
and ordinary (member) methods. A static field is a class-wide field and is shared
between all instances of the owning class. The field is accessible even if there are no
class Date {
int day, month, year;
}
http://professional-guru.com
12 Introduction to Java
instances of the class. Constructor methods have the same name as their associated
class. An appropriate constructor method is automatically called when objects of the
class are created. All methods of the object can also have associated modifiers. These
dictate the accessibility of the method; public, protected and private are
allowed. Public methods allow full access, protected allows access only from
within the same package or from with a subclass of the defining class and private
allows access only from within the defining class. Instance variables can also have
these modifiers. If no modifier is given, the access is restricted to the package. Static
methods (class-wide methods) can be invoked without reference to an object. They,
therefore, can only access static fields or other static methods.
Member methods and instance variables can have other modifiers which will be
introduced throughout this book.
The code for the Queue class can now be given.
package queues; // package name
import somepackage.Element; // import element type
class QueueNode { // class local to package
Element data; // queued data item
QueueNode next; // reference to next QueueNode
}
public class Queue {
// class available from outside the package
public Queue() { // public constructor
front = null;
back = null;
}
public void insert(Element E) { // visible method
QueueNode newNode = new QueueNode();
newNode.data = E;
newNode.next = null;
if(empty()) {
front = newNode;
} else {
back.next = newNode;
}
back = newNode;
}
http://professional-guru.com
Introduction to Java 13
Java 1.5 has introduced extra facilities for generic programming. The above example
assumed that the queue was for the Element class. To generalise this class so that it
deals with any element, the Element is defined as a generic parameter:
The queue can now be instantiated for a particular class.
public Element remove() { //visible method
if(!empty()) {
Element tmpE = front.data;
front = front.next;
if(empty()) back = null;
return tmpE;
}
// Garbage collection will free up the QueueNode
// object which is now dangling.
return null; // queue empty
}
public boolean empty() { // visible method
return (front == null);
}
private QueueNode front, back; // instance variables
}
package queues; // package name
class QueueNode<Element> { // class local to package
Element data; // generic queued data item
QueueNode next; // reference to next QueueNode
}
public class Queue<Element> {
// class available from outside the package
public Queue() { // public constructor
... // as before
}
.. // as before
}
http://professional-guru.com
14 Introduction to Java
8 Inheritance
Inheritance in Java is obtained by deriving one class from another. Multiple inheritance
is not supported although similar effects can be achieved using interfaces (see below).
Consider an example of a class which describes a two dimensional coordinate
system.
This class can now be extended to produce a new class by naming the base class in the
declaration of the derived class with the extends keyword. The new class is placed in
the same package1
Queue<String> dictionary = new Queue<String>();
package coordinates;
public class TwoDimensionalCoordinate {
public TwoDimensionalCoordinate(
float initialX, float initialY) { // constructor
X = initialX;
Y = initialY;
}
public void set(float F1, float F2) {
X = F1;
Y = F2;
}
public float getX() {
return X;
}
public float getY() {
return Y;
}
private float X;
private float Y;
}
http://professional-guru.com
Introduction to Java 15
A new field Z has been added and the constructor class has been defined, the set func-
tion has been overridden, and a new operation provided. Here, the constructor calls the
base class constructor (via the super keyword) and then initializes the final dimen-
sion. Similarly, the overloaded set function calls the base class.
All method calls in Java are potentially dispatching (dynamically bound). For
example, consider again the above example:
1. In Java, public classes must reside in their own file. Hence, a package can be distrib-
uted across one or more files, and can be continually augmented.
package coordinates;
public class ThreeDimensionalCoordinate
extends TwoDimensionalCoordinate {
// subclass of TwoDimensionalCoordinate
float Z; // new field
public ThreeDimensionalCoordinate(
float initialX, float initialY,
float initialZ) { // constructor
super(initialX, initialY);
// call superclass constructor
Z = initialZ;
}
public void set(float F1, float F2, float F3) {
//overloaded method
set(F1, F2); // call superclass set
Z = F3;
}
public float getZ() { // new method
return Z;
}
}
http://professional-guru.com
16 Introduction to Java
Here the method plot has been added. Now if plot is overridden in a child (sub)
class:
Then the following:
would plot a two dimensional coordinate; where as
package coordinates;
public class TwoDimensionalCoordinate {
// as before
public void plot() {
// plot a two dimensional point
}
}
package coordinates;
public class ThreeDimensionalCoordinate
extends TwoDimensionalCoordinate {
// as before
public void plot() {
// plot a three dimensional point
}
}
{
TwoDimensionalCoordinate A = new
TwoDimensionalCoordinate(0f, 0f);
A.plot();
}
http://professional-guru.com
Introduction to Java 17
would plot a three dimensional coordinate even though A was originally declared to be
of type TwoDimensionalCoordinate. This is because A and B are reference
types. By assigning B to A, only the reference has changed not the object itself.
The Object
class
All classes, in Java, are implicit subclasses of a root class called Object. The defini-
tion of this class is given below:
{
TwoDimensionalCoordinate A =
new TwoDimensionalCoordinate(0f, 0f);
ThreeDimensionalCoordinate B =
new ThreeDimensionalCoordinate(0f, 0f, 0f);
A = B;
A.plot();
}
package java.lang;
public class Object {
public final Class getClass();
public String toString();
public boolean equals(Object obj);
public int hashCode();
protected Object clone()
throws CloneNotSupportedException;
public final void wait()
throws InterruptedException;
// throws unchecked IllegalMonitorStateException
public final void wait(long millis)
throws InterruptedException;
// throws unchecked IllegalMonitorStateException
public final void wait(long millis, int nanos)
throws InterruptedException;
// throws unchecked IllegalMonitorStateException
http://professional-guru.com
18 Introduction to Java
There are six methods in the Object class which are of interest in this book. The three
wait and two notify methods are used for concurrency control and will be consid-
ered in Chapter 3.
The sixth method is the finalize method. It is this method that gets called just
before the object is destroyed. Hence by overriding this method, a child class can pro-
vide finalization for objects which are created from it. Of course, when a class over-
rides the finalize method, its last action should be to call the finalize method
of its parent. However, it should be noted that finalize is only called when garbage
collection is about to take place, and this may be some time after the object is no longer
in use; there are no explicit destructor methods as in, say, C++. Furthermore, garbage
collection will not necessarily take place before a program terminates. In the System
class there are two methods that can be used to request finalization:
The first of these methods requests the JVM to complete all outstanding finalization
code. The second method (when called with a true parameter) indicates that all out-
standing finalization be completed before the program exits; however, it has now been
deprecated as its use was found to lead to potential deadlocks.
public final void notify()
// throws unchecked IllegalMonitorStateException;
public final void notifyAll()
// throws unchecked IllegalMonitorStateException;
protected void finalize() throws Throwable;
}
package java.lang;
public class System {
...
public static void runFinalization();
public static void runFinalizeOnExit(boolean value);
// deprecated
}
http://professional-guru.com
Introduction to Java 19
9 Interfaces
Interfaces augment classes to increase the reusability of code. An interface defines a
reference type which contains a set of methods and constants. The methods are by defi-
nition abstract, so no instances of interfaces can be constructed. Instead, one or more
classes can implement an interface, and objects implementing interfaces can be passed
as arguments to methods by defining the parameter to be of the interface type. What
interfaces do, in effect, is to allow relationships to be constructed between classes out-
side of the class hierarchy.
Consider, a “generic” algorithm to sort arrays of objects. What is common about
all classes that can be sorted is that they support a less than, <, or greater than, >,
operator. Consequently, this feature is encapsulated in an interface. The Ordered
interface defines a single method lessThan that takes as an argument an object
whose class implements the Ordered interface. Any class which implements the
Ordered interface must compare its object’s ordering with the argument passed and
return whether it is less than the object or not.
The class for complex numbers is such a class.
package interfaceExamples;
public interface Ordered {
boolean lessThan (Ordered O);
}
import interfaceExamples.*;
class ComplexNumber implements Ordered {
// the class implements the Ordered interface
public boolean lessThan(Ordered O) {
// the interface implementation
ComplexNumber CN = (ComplexNumber) O; // cast O
if((realPart*realPart + imagPart*imagPart) <
(CN.getReal()*CN.getReal() +
CN.getImag()*CN.getImag())) {
return true;
}
return false;
}
http://professional-guru.com
20 Introduction to Java
Now it is possible to write algorithms which will sort this and any other class that
implements the interface. For example, an array sort:
The sort method takes two arguments; the first is an array of objects that implement
the Ordered interface and the second is the number of items in the array. The imple-
mentation performs an exchange sort. The important point about this example is that
when two objects are exchanged, it is the reference values which are exchanged and
public ComplexNumber (float I, float J) {
// constructor
realPart = I;
imagPart = J;
}
public float getReal() { return realPart; }
public float getImag() { return imagPart; }
protected float realPart;
protected float imagPart;
}
package interfaceExamples;
public class ArraySort {
public static void sort (Ordered[] oa) {
Ordered tmp;
int pos;
for (int i = 0; i < oa.length - 1; i++) {
pos = i;
for (int j = i + 1; j < oa.length; j++) {
if (oa[j].lessThan(oa[pos])) {
pos = j;
}
}
tmp = oa[pos];
oa[pos] = oa[i];
oa[i] = tmp;
}
}
}
http://professional-guru.com
Introduction to Java 21
hence it does not matter what type the object is (as long as it supports the Ordered
interface).
To use the above classes and interfaces simply requires the following
In fact, the package java.lang already defines an interface called Comparable
with a function called compareTo which could be used in place of Ordered. Fur-
thermore, there is a static method in java.util.Arrays called sort that imple-
ments a merge sort of objects.
Interfaces have three further properties which should be noted. Firstly, like
classes, they can participate in inheritance relationships with other interfaces. However,
unlike classes, multiple inheritance is allowed. Secondly, a class can implement more
than one interface, and hence, much of the functionality of multiple inheritance can be
achieved for classes. Finally, interfaces also provide the mechanisms by which call-
backs can be implemented. This is when an object (server) needs to call one or more
methods defined in the caller's (client’s) class. The client’s class can be of any type. As
Java does not support function pointers, it is necessary to provide an alternative mecha-
nism to that used traditionally in languages like C and C++. If the class implements an
interface which defines the required functions, then the client can pass itself as a
parameter to the server. The server can then call-back on any of the methods defined in
that interface.
{
ComplexNumber[] arrayComplex = { // for example
new ComplexNumber(6f,1f),
new ComplexNumber(1f, 1f),
new ComplexNumber(3f,1f),
new ComplexNumber(1f, 0f),
new ComplexNumber(7f,1f),
new ComplexNumber(1f, 8f),
new ComplexNumber(10f,1f),
new ComplexNumber(1f, 7f)
};
// array unsorted
ArraySort.sort(arrayComplex);
// array sorted
}
http://professional-guru.com
22 Introduction to Java
10 Inner classes
Inner classes are classes that are declared within other classes, they allow more control
over the visibility and give added flexibility. There are various types of inner classes,
the most important for this book are member, local and anonymous classes.
Member
classes
A member class is declared in the same way that a method or a field is declared. The
code of the member class can access all fields and methods of the enclosing class.
Local
classes
A local class is declared within a block of code within an enclosing class (such as
within a method of the enclosing class). It can access everything that a member class
can access. It can also access any final variables declared locally to the block declaring
the local class (and final parameters, if the block is a method).
public class Outer {
// local fields and methods of Outer
class MemberClass {
// Fields and methods of the Member class can
// access local fields and methods of the
// Outer class.
}
}
public class Outer {
// local fields and methods of Outer
void method(/* parameters */) {
// local variables
class LocalClass {
// Fields and methods of the LocalClass
// can access local fields and methods of
// the Outer class, and final fields or
// parameters of the declaring method.
}
}
}
http://professional-guru.com
Introduction to Java 23
Anonymous
classes
An anonymous class is a local class definition that has no name and is used to immedi-
ately create an instance of that class. It is often employed to create a class that imple-
ments an interface without having to give a name to the class definition.
11 Summary
This document has provided some necessary introductory material on Java for those not
familiar with the language but have experience in C, C++ and are aware of the basic
principles of object-oriented programming.for the remainder of the book. The book
assume that the reader is familiar with sequential programming in Java.
public class Outer
// local fields and methods on Inner
Ordered method(/* parameters */) {
// local variables
return new Ordered()
{
// Here is the code for the anonymous class
// that implements the Ordered interface.
// Its fields and methods can access local fields
// and methods of the Outer class, and final
// fields or parameters of the declaring method.
}
}
}
http://professional-guru.com

More Related Content

PPT
PPT
Annotations
PPT
Unit 5 Java
PPTX
OCA Java SE 8 Exam Chapter 2 Operators & Statements
PPT
Understanding Annotations in Java
PPTX
Java Annotations
PPTX
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
PPTX
Basics of Java
Annotations
Unit 5 Java
OCA Java SE 8 Exam Chapter 2 Operators & Statements
Understanding Annotations in Java
Java Annotations
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
Basics of Java

What's hot (20)

PPTX
Type Annotations in Java 8
PPTX
The Go Programing Language 1
PDF
Java Annotation Processing: A Beginner Walkthrough
PPT
Java basic tutorial by sanjeevini india
PPT
Java Basics
DOCX
Java se 8 fundamentals
PPT
Java Basics
DOCX
Java Interview Questions For Freshers
PPTX
OCA Java SE 8 Exam Chapter 5 Class Design
PPT
Java Annotation
PDF
Java programming basics
PPT
Unit 1 Java
PPT
Java tutorial PPT
PPT
JAVA BASICS
PPTX
Lecture - 2 Environment setup & JDK, JRE, JVM
PPTX
Actors model in gpars
PPT
Introduction to-programming
PPTX
Java interview questions 1
PDF
Java Faqs useful for freshers and experienced
PPT
Java API, Exceptions and IO
Type Annotations in Java 8
The Go Programing Language 1
Java Annotation Processing: A Beginner Walkthrough
Java basic tutorial by sanjeevini india
Java Basics
Java se 8 fundamentals
Java Basics
Java Interview Questions For Freshers
OCA Java SE 8 Exam Chapter 5 Class Design
Java Annotation
Java programming basics
Unit 1 Java
Java tutorial PPT
JAVA BASICS
Lecture - 2 Environment setup & JDK, JRE, JVM
Actors model in gpars
Introduction to-programming
Java interview questions 1
Java Faqs useful for freshers and experienced
Java API, Exceptions and IO
Ad

Similar to Introduction to JAVA (20)

PDF
java notes.pdf
PPTX
Java Jive 002.pptx
PPTX
ppt_on_java.pptx
PPT
java introduction
PPTX
Android webinar class_java_review
PPTX
JAVA AND OOPS CONCEPTS.pptx helpful for engineering
PPT
Core_java_ppt.ppt
PPTX
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
PPTX
Java programming(unit 1)
PPTX
Introduction to oop and java fundamentals
DOC
MC0078 SMU 2013 Fall session
PPTX
Core java
PPTX
Assignmentjsnsnshshusjdnsnshhzudjdndndjd
PPTX
Java UNITbgbgbfdbv v bbfbf cvbgfbvc gf 1.pptx
PDF
PJ_M01_C01_PPT_Introduction to Object Oriented Programming Using Java.pdf
PDF
Master Java Programming: Tips, Tutorials, and Best Practices
PPTX
brief introduction to core java programming.pptx
PPT
CSL101_Ch1.ppt
PPTX
CSL101_Ch1.pptx
PPTX
CSL101_Ch1.pptx
java notes.pdf
Java Jive 002.pptx
ppt_on_java.pptx
java introduction
Android webinar class_java_review
JAVA AND OOPS CONCEPTS.pptx helpful for engineering
Core_java_ppt.ppt
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
Java programming(unit 1)
Introduction to oop and java fundamentals
MC0078 SMU 2013 Fall session
Core java
Assignmentjsnsnshshusjdnsnshhzudjdndndjd
Java UNITbgbgbfdbv v bbfbf cvbgfbvc gf 1.pptx
PJ_M01_C01_PPT_Introduction to Object Oriented Programming Using Java.pdf
Master Java Programming: Tips, Tutorials, and Best Practices
brief introduction to core java programming.pptx
CSL101_Ch1.ppt
CSL101_Ch1.pptx
CSL101_Ch1.pptx
Ad

More from Professional Guru (20)

PPTX
introduction to AWs
PDF
introduction to AWs
PDF
Introduction to Java
PDF
Introduction to Big Data
PPT
Introduction to Azure
PDF
introduction to DEVOPS
PDF
Introduction to Azure
PDF
Introduction to Java
PDF
Introduction to Angular Js
PDF
Dev ops concept
PDF
Robotic Process Automation
PDF
Dev ops concept
PDF
Introduction to AWS
PDF
introduction to hadoop
PDF
Introduction to SQL SERVER
PDF
Introduction to Java
PDF
Introduction to Azure
PDF
Introduction to Angular Js
PDF
Introduction to HTML
PDF
Rpa developer resume
introduction to AWs
introduction to AWs
Introduction to Java
Introduction to Big Data
Introduction to Azure
introduction to DEVOPS
Introduction to Azure
Introduction to Java
Introduction to Angular Js
Dev ops concept
Robotic Process Automation
Dev ops concept
Introduction to AWS
introduction to hadoop
Introduction to SQL SERVER
Introduction to Java
Introduction to Azure
Introduction to Angular Js
Introduction to HTML
Rpa developer resume

Recently uploaded (20)

PPTX
Cell Types and Its function , kingdom of life
PDF
Classroom Observation Tools for Teachers
PDF
Business Ethics Teaching Materials for college
PDF
Basic Mud Logging Guide for educational purpose
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Cell Structure & Organelles in detailed.
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Insiders guide to clinical Medicine.pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Institutional Correction lecture only . . .
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Cell Types and Its function , kingdom of life
Classroom Observation Tools for Teachers
Business Ethics Teaching Materials for college
Basic Mud Logging Guide for educational purpose
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Cell Structure & Organelles in detailed.
2.FourierTransform-ShortQuestionswithAnswers.pdf
Pre independence Education in Inndia.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Final Presentation General Medicine 03-08-2024.pptx
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Insiders guide to clinical Medicine.pdf
01-Introduction-to-Information-Management.pdf
Institutional Correction lecture only . . .
Module 4: Burden of Disease Tutorial Slides S2 2025
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf

Introduction to JAVA

  • 1. Introduction to Java The Java architecture consists of: • a high-level object-oriented programming language, • a platform-independent representation of a compiled class, • a pre-defined set of run-time libraries, • a virtual machine. This book is mainly concerned with the language aspects of Java and the associated java.lang library package. Consequently, the remainder of this section provides a brief introduction to the language. Issues associated with the other components will be introduced as and when needed in the relevant chapters. The introduction is broken down into the following components • identifiers and primitive data types • structured data types • reference types • blocks and exception handling • control structures • procedures and functions • object oriented programming, packages and classes • inheritance • interfaces • inner classes. 1 Identifiers and primitive data types Identifiers Java does not restrict the lengths of identifiers. Although the language does allow the use of a “_” to be included in identifier names, the emerging style is to use a mixture of upper and lower case characters. The following are example identifiers: http://professional-guru.com
  • 2. 2 Introduction to Java Note that Java is case sensitive. Hence, the identifier exampleNameInJava is differ- ent from ExampleNameInJava. Primitive data types Java provides both a variety of discrete data types and a floating point type. Discrete data types. The following discrete data types are supported: • int — a 32-bit signed integer; • short — a 16-bit signed integer; • long — a 64-bit signed integer; • byte — an 8-bit signed integer; • boolean — the truth values, true and false; • char — Unicode characters (16 bits). All the usual operators for these types are available. Enumeration types have been introduces as of Java 1.5. Although not a primitive type it behaves like a descrete type and can be used in a switch statement. The follow- ing example illustrates a simple enumeration type for days of the week. Floating point numbers Java provides float and double types that support the 32 and 64 bit IEEE 754 float- ing point values respectively. Note, that floating point literals are automatically consid- ered to be of double precision. Consequently, they must either be explicitly converted to float or they can be followed by the letter f. For example: exampleNameInJava example_name_in_Java public enum Day{SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY} float bodyTemperature = (float) 98.6; // or float bodyTemperature = 98.6f; http://professional-guru.com
  • 3. Introduction to Java 3 Note that the assignment operator is “=” and that comments are delimited by “/*” and “*/”. Java also allows “//” for comments ending a line. 2 Structured data types Java supports arrays; both single and multi dimensional arrays can be created; for example: Note that arrays are represented by objects and that indexes start at 0. The length of the array can be determined by the length field associated with the array Java has no record structure as such. However, the same effect can be achieved using classes. The details of classes will be given in Section 7 but, for now, consider the following class for the date: It is not possible to express range constraints on the values of the date's components. Note also that as a “record” is a class, an allocator (the new operator) must be used to create an instance of the Date. Initialization of the object can be achieved using con- structors (see Section 7). Important note final int max = 10; // a constant float[] reading = new float[max]; // index is 0 .. max-1 boolean[][] switches = new boolean[max][max]; class Date { int day, month, year; } Date birthDate = new Date(); birthDate.day = 31; birthDate.month = 1; birthDate.year = 2000; All objects created by the new operator are stored in an area of memory called the heap. An activity called garbage collection will free up any associated memory when the object is no longer referenced. http://professional-guru.com
  • 4. 4 Introduction to Java 3 Reference types All objects in Java are references to the actual locations containing the encapsulated data, hence no additional access or pointer type is required. Furthermore, no forward declaration is required. For example: Note that as a result of representing objects as reference values, comparing two objects is a comparison between their references not their values. Hence, will compare the locations of the objects not the values encapsulated by the objects (in this case, the values of the value and next fields). To compare values requires a class to implement an explicit equals method. A similar situation occurs with object assignment. The assignment operator assigns to the reference. To create an actual copy of an object requires the class to provide methods to clone the object. 4 Blocks and exception handling In Java, a block (or compound statement) is delimited by “{“ and “}”, and usually has the following structure class Node { int value; Node next; // The type node can be used even though // its declaration has not been completed yet. } Node ref1 = new Node(); Node ref2 = new Node(); . . . if(ref1 == ref2) { ... } http://professional-guru.com
  • 5. Introduction to Java 5 although the declarative part can be dispersed throughout the block. Java can have exception handlers at the end of a block if the block is labeled as a try block. Each handler is specified using a catch statement. Consider, the following: The catch statement is like a function declaration, the parameter of which identifies the exception type to be caught. Inside the handler, the object name behaves like a local variable. A handler with parameter type T will catch a thrown object of type E if: • T and E are the same type, • or T is a parent (super) class of E at the throw point. It is this last point that integrates the exception handling facility with the object-ori- ented programming model. In the above example the Exception class is the root class of all application exceptions. Hence, the catch clause will catch all application exceptions. Here, getMessage is a method declared in the Exception class. A call to E.getMessage will execute the appropriate routine for the type of object thrown. If no exception handler is found in the calling context of a function, the calling context is terminated and a handler is sought in its calling context. Hence, Java supports exception propagation. Finally clauses Java also supports a finally clause as part of a try statement. The code attached to this clause is guaranteed to execute whatever happens in the try statement irrespective { < declarative part > < sequence of statements > } try { //code that might throw an exception } catch (Exception err) { // Exception caught, print error message on // the standard output. System.out.println(err.getMessage()); } http://professional-guru.com
  • 6. 6 Introduction to Java of whether exceptions are thrown, caught, propagated or, indeed, even if there are no exceptions thrown at all. Checked and unchecked exceptions In Java, all exceptions are subclasses of the predefined class java.lang.Throw- able. The language also defines other classes, for example: Error, Exception, and RuntimeException. The relationship between these (and some common exceptions) is depicted in Figure 1. FIGURE 1. Part of the The Java Predefined Throwable Class Hierarchy Throughout this book, the term Java exception is used to denote any class derived from Exception. Objects derived from Error describe internal errors and resource exhaustion in the Java run-time support system. Although these errors clearly have a major impact on the program, there is little that the program can do when they are try { ... } catch(...) { ... } finally { // code that will be executed under all circumstances } Throwable Error Exception RunTimeException IllegalArgumentException IllegalThreadStateException SecurityException InterruptedException NullPointerException checked unchecked IllegalMonitorStateException http://professional-guru.com
  • 7. Introduction to Java 7 thrown (raised) as no assumptions can be made concerning the integrity of the system. Objects derived from the Exception hierarchy represent errors that programs can handle or can throw themselves. RuntimeExceptions are those exceptions that are raised by the run-time system as a result of a program error. They include errors such as those resulting from a bad cast (ClassCastException), array bounds error (IndexOutOfBoundException), a null pointer access (NullPointerExcep- tion), integer divide by zero (ArithmeticException) etc. Throwable objects which are derived from Error or RuntimeExceptions are called unchecked exceptions, the others are termed checked exceptions. Checked exceptions must be declared by the function that can throw them. The compiler will check that an appropriate handler can be found. The compiler makes no attempt to ensure that handlers for unchecked exceptions exist. 5 Control structures Control structures can be grouped together into three categories: sequences, decisions and loops. Sequence structures Most languages implicitly require sequential execution, and no specific control struc- ture is provided. The definitions of a block in Java indicate that between { and } there is a sequence of statements. Execution is required to follow this sequence. Decision structures The most common form of decision structure is the if statement; for example: In general, a multiway decision can be more explicitly stated and efficiently imple- mented, using a switch structure. if(A != 0) { if(B/A > 10) { high = 1; } else { high = 0; } } http://professional-guru.com
  • 8. 8 Introduction to Java As with C and C++, it is necessary to insert statements to break out of the switch once the required command has been identified. Without these, control continues to the next option (as with the case of A). Loop (iteration) structures Java supports the usual for and while statements. The following example illustrates the for statement; the code assigns into the first ten elements of array, A, the value of their positions in the array: The free use of the loop control variable (i here) in this manner can be the cause of many errors, consequently, Java also allows a local variable to be declared inside the for loop. An example while statement is given below: switch(command) { case 'A' : case 'a' : action1(); break; /* A or a */ case 't' : action2(); break; case 'e' : action3(); break; case 'x' : case 'y' : case 'z' : action4(); break; /* x, y or z */ default : /* no action */ } for(i = 0; i <= 9; i++) { /* i must be previously declared */ A[i]= i; /* i can be read/written in the loop */ } /* the value of i is defined */ /* after the loop */ for(int i = 0; i <= max; i++) { A[i]= i; } http://professional-guru.com
  • 9. Introduction to Java 9 Java also supports a variant where the test occurs at the end of the loop: The flexibility of an iteration statement is increased by allowing control to pass out of the loop (that is, the loop to terminate) from any point within it (break) or for control to pass directly to the next iteration (continue): 6 Procedures and functions Procedures and functions can only be declared in the context of a class, they are known collectively as methods. In fact, strictly Java only supports functions; a procedure is considered to be a function with no return value (indicated by void in the function definition). Java passes primitive data type (int, boolean, float etc.) parameters (often called arguments) by value. Variables of class type are reference variables. Hence, when they are passed as arguments they are copied, but the effect is as if the object was passed by reference. Consider a function that calculates the roots of a quadratic equation: while(<expression>) { /* Expression evaluating to true or false. */ /* False implies loop termination. */ <sequence of statements> } do { < sequence of statements> } while (<expression>); while(true) { ... if(<expression1>) break; if(<expression2>) continue; ... } http://professional-guru.com
  • 10. 10 Introduction to Java Note Java requires the roots of the equation to be passed as a class type, as primitive types (including double) are passed by copy and there are no pointer types. Function bodies The bodies of functions are illustrated by completing the quadratic definitions given above. The invoking of a function merely involves naming the function (including any object or class name) and giving the appropriately typed parameters in parentheses. 7 Object-oriented programming, packages and classes Objects have four important properties [Wegner, 1987]. They have • inheritance (type extensibility) public class Roots { double R1, R2; } // the following must be declared in a class context boolean quadratic(double A, double B, double C, Roots R); boolean quadratic(double A, double B, double C, Roots R) { double disc; disc = B*B - 4.0*A*C; if(disc < 0.0 || A == 0.0) { // no roots R.R1 = 0; // arbitrary values R.R2 = 0; return false; } R.R1 = (-B + Math.sqrt(disc)) / (2.0*A); R.R2 = (-B - Math.sqrt(disc)) / (2.0*A); return true; } http://professional-guru.com
  • 11. Introduction to Java 11 • automatic object initialization (constructors) • automatic object finalization (destructors) • dynamic binding of method calls (polymorphism — sometimes called run-time dispatching of operations). All of which are supported, in some form, by Java’s class and interface mechanisms. Related classes and interfaces can be grouped together into larger units called pack- ages. Inheritance is perhaps the most significant concept in an object abstraction. This enables a type (class) to be defined as an extension of a previously defined type. The new type inherits the “base” type but may include new fields and new operations. Once the type has been extended then run-time dispatching of operations is required to ensure the appropriate operation is called for a particular instance of the family of types. Earlier in this section, a simple class for the date type was introduced. This example only illustrates how data items can be grouped together. The full facilities of a class allow the data items (or instance variables or fields as Java calls them) to be encapsulated and, hence, abstract data types to be defined. As an example, consider the class for a queue abstraction. First, a package to contain the queue can be declared (if there is no named pack- age, then the system assumes an unnamed package). Items from other packages can be imported. Then classes to be declared inside the package are given. One of these classes Queue is declared as public, and it is only this that can be accessed outside the package. The keyword public is termed a modifier in Java, the other ones for classes include abstract and final. An abstract class is one from which no objects can be created. Hence, the class has to be extended (to produce a subclass) and that subclass made non-abstract before objects can exist. A final modifier indicates that the class cannot be subclassed. No modifier indicates that the class is only accessi- ble within the package. A class can have more than one modifier but certain combina- tions, such as abstract and final, are meaningless. Each class can declare local instance variables (or fields), constructor methods and ordinary (member) methods. A static field is a class-wide field and is shared between all instances of the owning class. The field is accessible even if there are no class Date { int day, month, year; } http://professional-guru.com
  • 12. 12 Introduction to Java instances of the class. Constructor methods have the same name as their associated class. An appropriate constructor method is automatically called when objects of the class are created. All methods of the object can also have associated modifiers. These dictate the accessibility of the method; public, protected and private are allowed. Public methods allow full access, protected allows access only from within the same package or from with a subclass of the defining class and private allows access only from within the defining class. Instance variables can also have these modifiers. If no modifier is given, the access is restricted to the package. Static methods (class-wide methods) can be invoked without reference to an object. They, therefore, can only access static fields or other static methods. Member methods and instance variables can have other modifiers which will be introduced throughout this book. The code for the Queue class can now be given. package queues; // package name import somepackage.Element; // import element type class QueueNode { // class local to package Element data; // queued data item QueueNode next; // reference to next QueueNode } public class Queue { // class available from outside the package public Queue() { // public constructor front = null; back = null; } public void insert(Element E) { // visible method QueueNode newNode = new QueueNode(); newNode.data = E; newNode.next = null; if(empty()) { front = newNode; } else { back.next = newNode; } back = newNode; } http://professional-guru.com
  • 13. Introduction to Java 13 Java 1.5 has introduced extra facilities for generic programming. The above example assumed that the queue was for the Element class. To generalise this class so that it deals with any element, the Element is defined as a generic parameter: The queue can now be instantiated for a particular class. public Element remove() { //visible method if(!empty()) { Element tmpE = front.data; front = front.next; if(empty()) back = null; return tmpE; } // Garbage collection will free up the QueueNode // object which is now dangling. return null; // queue empty } public boolean empty() { // visible method return (front == null); } private QueueNode front, back; // instance variables } package queues; // package name class QueueNode<Element> { // class local to package Element data; // generic queued data item QueueNode next; // reference to next QueueNode } public class Queue<Element> { // class available from outside the package public Queue() { // public constructor ... // as before } .. // as before } http://professional-guru.com
  • 14. 14 Introduction to Java 8 Inheritance Inheritance in Java is obtained by deriving one class from another. Multiple inheritance is not supported although similar effects can be achieved using interfaces (see below). Consider an example of a class which describes a two dimensional coordinate system. This class can now be extended to produce a new class by naming the base class in the declaration of the derived class with the extends keyword. The new class is placed in the same package1 Queue<String> dictionary = new Queue<String>(); package coordinates; public class TwoDimensionalCoordinate { public TwoDimensionalCoordinate( float initialX, float initialY) { // constructor X = initialX; Y = initialY; } public void set(float F1, float F2) { X = F1; Y = F2; } public float getX() { return X; } public float getY() { return Y; } private float X; private float Y; } http://professional-guru.com
  • 15. Introduction to Java 15 A new field Z has been added and the constructor class has been defined, the set func- tion has been overridden, and a new operation provided. Here, the constructor calls the base class constructor (via the super keyword) and then initializes the final dimen- sion. Similarly, the overloaded set function calls the base class. All method calls in Java are potentially dispatching (dynamically bound). For example, consider again the above example: 1. In Java, public classes must reside in their own file. Hence, a package can be distrib- uted across one or more files, and can be continually augmented. package coordinates; public class ThreeDimensionalCoordinate extends TwoDimensionalCoordinate { // subclass of TwoDimensionalCoordinate float Z; // new field public ThreeDimensionalCoordinate( float initialX, float initialY, float initialZ) { // constructor super(initialX, initialY); // call superclass constructor Z = initialZ; } public void set(float F1, float F2, float F3) { //overloaded method set(F1, F2); // call superclass set Z = F3; } public float getZ() { // new method return Z; } } http://professional-guru.com
  • 16. 16 Introduction to Java Here the method plot has been added. Now if plot is overridden in a child (sub) class: Then the following: would plot a two dimensional coordinate; where as package coordinates; public class TwoDimensionalCoordinate { // as before public void plot() { // plot a two dimensional point } } package coordinates; public class ThreeDimensionalCoordinate extends TwoDimensionalCoordinate { // as before public void plot() { // plot a three dimensional point } } { TwoDimensionalCoordinate A = new TwoDimensionalCoordinate(0f, 0f); A.plot(); } http://professional-guru.com
  • 17. Introduction to Java 17 would plot a three dimensional coordinate even though A was originally declared to be of type TwoDimensionalCoordinate. This is because A and B are reference types. By assigning B to A, only the reference has changed not the object itself. The Object class All classes, in Java, are implicit subclasses of a root class called Object. The defini- tion of this class is given below: { TwoDimensionalCoordinate A = new TwoDimensionalCoordinate(0f, 0f); ThreeDimensionalCoordinate B = new ThreeDimensionalCoordinate(0f, 0f, 0f); A = B; A.plot(); } package java.lang; public class Object { public final Class getClass(); public String toString(); public boolean equals(Object obj); public int hashCode(); protected Object clone() throws CloneNotSupportedException; public final void wait() throws InterruptedException; // throws unchecked IllegalMonitorStateException public final void wait(long millis) throws InterruptedException; // throws unchecked IllegalMonitorStateException public final void wait(long millis, int nanos) throws InterruptedException; // throws unchecked IllegalMonitorStateException http://professional-guru.com
  • 18. 18 Introduction to Java There are six methods in the Object class which are of interest in this book. The three wait and two notify methods are used for concurrency control and will be consid- ered in Chapter 3. The sixth method is the finalize method. It is this method that gets called just before the object is destroyed. Hence by overriding this method, a child class can pro- vide finalization for objects which are created from it. Of course, when a class over- rides the finalize method, its last action should be to call the finalize method of its parent. However, it should be noted that finalize is only called when garbage collection is about to take place, and this may be some time after the object is no longer in use; there are no explicit destructor methods as in, say, C++. Furthermore, garbage collection will not necessarily take place before a program terminates. In the System class there are two methods that can be used to request finalization: The first of these methods requests the JVM to complete all outstanding finalization code. The second method (when called with a true parameter) indicates that all out- standing finalization be completed before the program exits; however, it has now been deprecated as its use was found to lead to potential deadlocks. public final void notify() // throws unchecked IllegalMonitorStateException; public final void notifyAll() // throws unchecked IllegalMonitorStateException; protected void finalize() throws Throwable; } package java.lang; public class System { ... public static void runFinalization(); public static void runFinalizeOnExit(boolean value); // deprecated } http://professional-guru.com
  • 19. Introduction to Java 19 9 Interfaces Interfaces augment classes to increase the reusability of code. An interface defines a reference type which contains a set of methods and constants. The methods are by defi- nition abstract, so no instances of interfaces can be constructed. Instead, one or more classes can implement an interface, and objects implementing interfaces can be passed as arguments to methods by defining the parameter to be of the interface type. What interfaces do, in effect, is to allow relationships to be constructed between classes out- side of the class hierarchy. Consider, a “generic” algorithm to sort arrays of objects. What is common about all classes that can be sorted is that they support a less than, <, or greater than, >, operator. Consequently, this feature is encapsulated in an interface. The Ordered interface defines a single method lessThan that takes as an argument an object whose class implements the Ordered interface. Any class which implements the Ordered interface must compare its object’s ordering with the argument passed and return whether it is less than the object or not. The class for complex numbers is such a class. package interfaceExamples; public interface Ordered { boolean lessThan (Ordered O); } import interfaceExamples.*; class ComplexNumber implements Ordered { // the class implements the Ordered interface public boolean lessThan(Ordered O) { // the interface implementation ComplexNumber CN = (ComplexNumber) O; // cast O if((realPart*realPart + imagPart*imagPart) < (CN.getReal()*CN.getReal() + CN.getImag()*CN.getImag())) { return true; } return false; } http://professional-guru.com
  • 20. 20 Introduction to Java Now it is possible to write algorithms which will sort this and any other class that implements the interface. For example, an array sort: The sort method takes two arguments; the first is an array of objects that implement the Ordered interface and the second is the number of items in the array. The imple- mentation performs an exchange sort. The important point about this example is that when two objects are exchanged, it is the reference values which are exchanged and public ComplexNumber (float I, float J) { // constructor realPart = I; imagPart = J; } public float getReal() { return realPart; } public float getImag() { return imagPart; } protected float realPart; protected float imagPart; } package interfaceExamples; public class ArraySort { public static void sort (Ordered[] oa) { Ordered tmp; int pos; for (int i = 0; i < oa.length - 1; i++) { pos = i; for (int j = i + 1; j < oa.length; j++) { if (oa[j].lessThan(oa[pos])) { pos = j; } } tmp = oa[pos]; oa[pos] = oa[i]; oa[i] = tmp; } } } http://professional-guru.com
  • 21. Introduction to Java 21 hence it does not matter what type the object is (as long as it supports the Ordered interface). To use the above classes and interfaces simply requires the following In fact, the package java.lang already defines an interface called Comparable with a function called compareTo which could be used in place of Ordered. Fur- thermore, there is a static method in java.util.Arrays called sort that imple- ments a merge sort of objects. Interfaces have three further properties which should be noted. Firstly, like classes, they can participate in inheritance relationships with other interfaces. However, unlike classes, multiple inheritance is allowed. Secondly, a class can implement more than one interface, and hence, much of the functionality of multiple inheritance can be achieved for classes. Finally, interfaces also provide the mechanisms by which call- backs can be implemented. This is when an object (server) needs to call one or more methods defined in the caller's (client’s) class. The client’s class can be of any type. As Java does not support function pointers, it is necessary to provide an alternative mecha- nism to that used traditionally in languages like C and C++. If the class implements an interface which defines the required functions, then the client can pass itself as a parameter to the server. The server can then call-back on any of the methods defined in that interface. { ComplexNumber[] arrayComplex = { // for example new ComplexNumber(6f,1f), new ComplexNumber(1f, 1f), new ComplexNumber(3f,1f), new ComplexNumber(1f, 0f), new ComplexNumber(7f,1f), new ComplexNumber(1f, 8f), new ComplexNumber(10f,1f), new ComplexNumber(1f, 7f) }; // array unsorted ArraySort.sort(arrayComplex); // array sorted } http://professional-guru.com
  • 22. 22 Introduction to Java 10 Inner classes Inner classes are classes that are declared within other classes, they allow more control over the visibility and give added flexibility. There are various types of inner classes, the most important for this book are member, local and anonymous classes. Member classes A member class is declared in the same way that a method or a field is declared. The code of the member class can access all fields and methods of the enclosing class. Local classes A local class is declared within a block of code within an enclosing class (such as within a method of the enclosing class). It can access everything that a member class can access. It can also access any final variables declared locally to the block declaring the local class (and final parameters, if the block is a method). public class Outer { // local fields and methods of Outer class MemberClass { // Fields and methods of the Member class can // access local fields and methods of the // Outer class. } } public class Outer { // local fields and methods of Outer void method(/* parameters */) { // local variables class LocalClass { // Fields and methods of the LocalClass // can access local fields and methods of // the Outer class, and final fields or // parameters of the declaring method. } } } http://professional-guru.com
  • 23. Introduction to Java 23 Anonymous classes An anonymous class is a local class definition that has no name and is used to immedi- ately create an instance of that class. It is often employed to create a class that imple- ments an interface without having to give a name to the class definition. 11 Summary This document has provided some necessary introductory material on Java for those not familiar with the language but have experience in C, C++ and are aware of the basic principles of object-oriented programming.for the remainder of the book. The book assume that the reader is familiar with sequential programming in Java. public class Outer // local fields and methods on Inner Ordered method(/* parameters */) { // local variables return new Ordered() { // Here is the code for the anonymous class // that implements the Ordered interface. // Its fields and methods can access local fields // and methods of the Outer class, and final // fields or parameters of the declaring method. } } } http://professional-guru.com