We are unable to create an online viewer for this document. Please download the document instead.
Programming in Java
by
Willi-Hans Steeb
International School for Scienti?c Computing
email addresses of the author:
steeb_wh@yahoo.com
Willi-Hans.Steeb@fhso.ch
whs@na.rau.ac.za
Contents
1 Introduction
1
1.1 Why Java ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
1.2 Aim of Object-Oriented Programming . . . . . . . . . . . . . . . . .
2
1.2.1
Information Hiding . . . . . . . . . . . . . . . . . . . . . . . .
3
1.2.2
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
1.2.3
Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
1.2.4
Built-In Classes . . . . . . . . . . . . . . . . . . . . . . . . . .
4
1.2.5
Java Compared To C++ . . . . . . . . . . . . . . . . . . . . .
5
1.3 Identi?ers and Keywords in Java . . . . . . . . . . . . . . . . . . . .
6
2 Java Basics
7
2.1 My First Java Program . . . . . . . . . . . . . . . . . . . . . . . . . .
7
2.2 My First Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9
2.3 Basic Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.4 Arithmetic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.5 Unicode, ASCII Table, and Types Conversion . . . . . . . . . . . . . 18
2.6 Precedence Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.7 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.8 Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.8.1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.8.2
The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.8.3
The for Loop, while Loop, do-while Loop . . . . . . . . . . . . 31
2.8.4
The switch Statement . . . . . . . . . . . . . . . . . . . . . . 35
2.9 Logical AND, Logical OR and Logical NOT . . . . . . . . . . . . . . 38
2.10 Bitwise Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.11 Shift Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
2.12 Pass by Value, Pass by Reference . . . . . . . . . . . . . . . . . . . . 43
2.13 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.14 Jump Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
2.15 Reading from Keyboard . . . . . . . . . . . . . . . . . . . . . . . . . 54
2.16 Command Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . 56
2.17 System Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
2.18 Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
2.19 Applets and HTML Parameters . . . . . . . . . . . . . . . . . . . . . 62
i
3 String Manipulations
64
3.1 String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
3.2 StringTokenizer Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
3.3 StringBu?er Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
4 Classes and Objects
73
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
4.2 Wrapper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
4.3 Vector Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
4.4 Math Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
4.5 BitSet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
4.6 BigInteger and BigDecimal Class . . . . . . . . . . . . . . . . . . . . 83
4.7 Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
4.8 The this Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
4.9 The Class Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
4.10 The Calendar Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
4.11 Destroying Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
4.12 Regular Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
4.13 The Bytecode Format . . . . . . . . . . . . . . . . . . . . . . . . . . 103
5 Inheritance and Abstract Classes
114
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
5.2 Abstract Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
5.3 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
5.4 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
5.5 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
5.6 Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
5.7 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
6 The GUI and its Components
132
6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
6.2 Class Component and Class Container . . . . . . . . . . . . . . . . . 135
6.3 ActionListener and ActionEvent . . . . . . . . . . . . . . . . . . . . . 139
6.4 Class Panel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
6.5 Mouse Listener and Mouse Event . . . . . . . . . . . . . . . . . . . . 148
6.6 Class Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
6.7 Graphics2D Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
6.8 Color Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
6.9 Class Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
6.10 Class Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
7 Exception Handling
173
7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
7.2 The Exception Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
7.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
ii
8 File Manipulations
179
8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
8.2 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
8.3 FileReader and FileWriter . . . . . . . . . . . . . . . . . . . . . . . . 194
8.4 File Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
8.5 Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
8.6 GZIP and ZIP Compression . . . . . . . . . . . . . . . . . . . . . . . 211
8.7 JPEG Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
8.8 Internationalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
8.9 Locking Files for Shared Access . . . . . . . . . . . . . . . . . . . . . 222
8.10 Security API, Signature and DSA Algorithm . . . . . . . . . . . . . . 224
9 Threads
228
9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
9.2 Thread Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
9.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
9.4 Priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
9.5 Synchronization and Locks . . . . . . . . . . . . . . . . . . . . . . . . 240
9.6 Producer Consumer Problem . . . . . . . . . . . . . . . . . . . . . . . 245
9.7 Deadlock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
10 Animation
250
10.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
10.2 Image Class and Animation . . . . . . . . . . . . . . . . . . . . . . . 252
10.3 AudioClip Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
11 Networking
262
11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
11.2 Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
11.3 Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
11.4 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
11.5 URL Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
11.6 Socket Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
11.7 Client-Server Application . . . . . . . . . . . . . . . . . . . . . . . . . 280
11.8 Remote Method Invocation . . . . . . . . . . . . . . . . . . . . . . . 288
11.9 SocketChannel and ServerSocketChannel . . . . . . . . . . . . . . . . 297
12 Java 2 Collection Frame Work
302
12.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
12.2 Collection and Collections . . . . . . . . . . . . . . . . . . . . . . . . 305
12.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
12.4 Arrays Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
12.5 Class TreeSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
12.6 Class TreeMap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
iii
13 The Swing Components
318
13.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
13.2 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
13.3 Adding Actions to Containers . . . . . . . . . . . . . . . . . . . . . . 323
13.4 Button Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
13.5 Editable Text?elds . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
13.6 Document and JTextPane Classes . . . . . . . . . . . . . . . . . . . . 333
13.7 Model-View-Controller . . . . . . . . . . . . . . . . . . . . . . . . . . 336
13.8 JTree Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
13.9 Class JEditorPane . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
13.10Printing in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
14 Java Beans
351
14.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
14.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
15 Additions to JSE 1.5
356
16 Resources and Web Sites
363
Bibliography
365
Index
365
iv
Preface
Besides C++ Java is now the most widely available and used object-oriented pro-
gramming language. It is a very useful language that is successfully utilized by
many programmers in many application areas. It is a reasonably carefully thought-
out language where the design is based partly on acknowledged principles and partly
on solid experience and feedback from actual use. Java is a powerful but lean object-
oriented programming language. It makes it possible to program for the Internet by
creating applets, i.e. programs that can be embedded in a web page. For example,
an applet can be an animation with sound, an interactive game, or a ticker tape
with constantly updated stock prices. However Java is more than a programming
language for writing applets. It also can be used for writing standalone applications.
It seems it is becoming the standard language for both general-purpose and Internet
programming. Java is close to C++. It has taken many features of C++, but unfor-
tunately discarded some of them, for example templates and multiple inheritence.
To this lean core it has added garbage collection (automatic memory management),
multithreading (the capacity for one program to do more than one task at the time),
and security capabilities. Java is a platform consisting of three components: (1) the
Java programming language, (2) the Java library of classes and interfaces (Java has
a huge number of built-in classes and interfaces), and (3) the Java Virtual Machine.
One of the biggest advantages Java o?ers is that it is portable. An application
written in Java will run on all platforms. Any computer with a Java-based browser
can run the applications or applets written in the Java programming language.
The Java Virtual Machine (JVM) is what gives Java its cross-platform capabilities.
The Java ?le is not compiled into a machine language, which is di?erent for each
operating system and computer architecture, Java code is compiled into byte-code
(platform independent).
The Java programming language is object-oriented, which makes program design
focus on what we are dealing with rather than on how we are going to do something.
Object-oriented languages use the paradigm of classes. A class is an abstract data
type (ADT). A class includes both data and the methods (functions) to operate on
that data. We can create an instance of class, also called an object, which will have
all the data members and functionality of its class. The class paradigm allows one
to encapsulate data so that speci?c data values or methods implementations cannot
be seen by those using the class. Encapsulation makes it possible to make changes
in code without breaking other programs that use that code. Java also includes
inheritance, this means the ability to derive new classes from existing classes. The
derived class, also called a subclass, inherits all the data and methods of the existing
class, referred to as the parent class. A subclass can add new data members to those
inherited from the parent class. With respect to methods the subclass can reuse the
inherited methods as is, change them, and/or add its own new method.
v
Java includes a huge number of built-in classes and interfaces. The programmer
can use already existing class as is, create subclasses to modify existing classes, or
implement interfaces to augment the capabilities of classes.
In chapter 2 we give the basic concepts in Java. The given programs are very helpful
for the beginners. They are also the building blocks for more complex applications.
The widely used String class and the classes StringTokenizer and StringBuffer
are introduced. Chapter 3 is devoted to classes and objects. Furthermore the
wrapper classes, the container class Vector, the class Math for doing mathematics
and the BigInteger and BigDecimal classes are introduced. The Object class is the
ancestor of all classes and discussed in detail in section 3.6. Finally the this object
is explained in detail. Chapter 4 deals with inheritance and abstract classes. The
graphical user interface (GUI) is discussed in chapter 5 and a number of examples
are provided. Chapter 6 introduces exception handling. File manipulations for
reading from ?le and writing to ?les are introduced in chapter 7. Java is able
to produce multi-threaded applications, which often form a part of applications
including animations. Threads are discussed in chapter 8 and application of threads
in animation are given in chapter 9. An introduction into networking together with
a number of programs is given in chapter 10. Chapters 11 and 12 deal with the
additions to Java for the version 1.2. Finally chapter 13 lists important Web sites
for Java, JavaScript and HTML.
The level of presentation is such that one can study the subject early on in ones
education in programming. There is a balance between practical programming and
the underlying language. The book is ideally suited for use in lectures on Java and
object-oriented programming. The beginner will also bene?t from the book. The
reference list gives a collection of textbooks useful in the study of the computer
language Java. There are a number of good textbooks for Java available [1], [2]. For
applications of Java in science we refer to Tan Kiat Shi, W.-H. Steeb and Yorick
Hardy [6] and Steeb [5]. Comprehensive introductions into JavaScript are given by
[3] and [4].
vi
Without doubt, this book can be extended. If you have comments or suggestions,
we would be pleased to have them. The email addresses of the author are:
whs@na.rau.ac.za
steeb_wh@yahoo.com
The web sites of the author are:
http://zeus.rau.ac.za
http://issc.rau.ac.za
vii
Chapter 1
Introduction
1.1
Why Java ?
Java is a platform-independent object-oriented, multi-threading dynamically-linked
programming language. Java was developed by Sun MicroSystems primarily for the
use on the World-Wide-Web. It has a lot in common with C++. With Java we can
produce three distinct types of programs, applets, applications, and beans.
Firstly, Java supports the central concepts of object-orientated programming: en-
capsulation, inheritance and polymorphism (including dynamic binding). It has
good support for dynamic memory management and supports both, procedural and
object-orientated programming.
However, other well-designed programming languages have failed against relatively
poor competitors. Being a good programming language is not su?cient for sur-
vival. An additional important requirement for a powerful programming language
is portability. If a ?rm replaces one computer system with another, only a minimal
amount of recording (if any) should be required. Java compilers are available for
virtually all machines and a high level of compatibility is ensured by the standard
for Java.
All these points have contributed to Java being the fastest growing computer lan-
guage for nearly all computer and operating systems and for nearly all software
applications, ranging from scienti?c to administrative programs to real-time indus-
trial applications and computer games.
1
2
CHAPTER 1. INTRODUCTION
1.2
Aim of Object-Oriented Programming
Object-oriented programming is the most dramatic innovation in software develop-
ment in the last two decades. It o?ers a new and powerful way to cope with the
complexity of programs. Using object-oriented programming we can write clearer,
more reliable, more easily maintained programs. Integrating data and functions
(methods) is the central idea of object-oriented programming.
The class is the foundation of Java’s support for object-oriented programming, and
is at the core of many of its advanced features. The class provides the mechanism
by which objects are created. Thus a class de?nes a new data type, which can be
used to create objects. A class is created by using the keyword class.
Object-oriented programming methods aim to achieve the following:
• To simplify the design and implementation of complex programs.
• To make it easier for teams of designers and programmers to work on a single
software project.
• To enable a high degree of reusability of designs and of software codes.
• To decrease the cost of software maintenance.
In order to achieve these aims, object-oriented programming languages are expected
to support a number of features:
1. Information hiding (encapsulation)
2. Polymorphism
3. Inheritance
Each of these concepts is discussed below.
Add New Comment