Javanote 11
Javanote 11
xiii
xiv Preface
of programming to cover more advanced topics. Chapter 8 is mostly about writing robust and
correct programs, but it also has a section on parallel processing and threads. Chapters 9 and
10 cover recursion and data structures, including the Java Collection Framework. Chapter 11 is
about files and networking. Finally, Chapter 12 returns to the topic of graphical user interface
programming to cover some of Java’s more advanced capabilities.
∗ ∗ ∗
Major changes have been made in the fifth edition. Perhaps the most significant change is
the use of parameterized types in the chapter on generic programming. Parameterized types—
Java’s version of templates—were the most eagerly anticipated new feature in Java 5.0.
Other new features in Java 5.0 are also covered. Enumerated types are introduced, although
they are not covered in their full complexity. The “for-each” loop is covered and is used
extensively. Formatted output is also used extensively, and the Scanner class is covered (though
not until Chapter 11). Static import is covered briefly, as are variable arity methods.
The non-standard TextIO class that I use for input in the first half of the book has been
rewritten to support formatted output. I have also added some file I/O capabilities to this class
to make it possible to cover some examples that use files early in the book.
Javadoc comments are covered for the first time in this edition. Almost all code examples
have been revised to use Javadoc-style comments.
The coverage of graphical user interface programming has been reorganized, much of it has
been rewritten, and new material has been added. In previous editions, I emphasized applets.
Stand-alone GUI applications were covered at the end, almost as an afterthought. In the fifth
edition, the emphasis on applets is gone, and almost all examples are presented as stand-alone
applications. However, applet versions of each example are still presented on the web pages of
the on-line version of the book. The chapter on advanced GUI programming has been moved
to the end, and a significant amount of new material has been added, including coverage of
some of the features of Graphics2D.
Aside from the changes in content, the appearance of the book has been improved, especially
the appearance of the PDF version. For the first time, the quality of the PDF approaches that
of conventional textbooks.
∗ ∗ ∗
The latest complete edition of Introduction to Programming using Java is always available
on line at http://math.hws.edu/javanotes/. The first version of the book was written in 1996,
and there have been several editions since then. All editions are archived at the following Web
addresses:
• First edition: http://math.hws.edu/eck/cs124/javanotes1/ (Covers Java 1.0.)
• Second edition: http://math.hws.edu/eck/cs124/javanotes2/ (Covers Java 1.1.)
• Third edition: http://math.hws.edu/eck/cs124/javanotes3/ (Covers Java 1.1.)
• Fourth edition: http://math.hws.edu/eck/cs124/javanotes4/ (Covers Java 1.4.)
• Fifth edition: http://math.hws.edu/eck/cs124/javanotes5/ (Covers Java 5.0.)
Introduction to Programming using Java is free, but it is not in the public domain. As
of Version 5.0, it is published under the terms of the Creative Commons Attribution-Share
Alike 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-
sa/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco,
California, 94105, USA. This license allows redistribution and modification under certain terms.
For example, you can:
Preface xv
• Post an unmodified copy of the on-line version on your own Web site (including the parts
that list the author and state the license under which it is distributed!).
• Give away or sell printed, unmodified copies of this book, as long as they meet the re-
quirements of the license.
• Make modified copies of the complete book or parts of it and post them on the web or
otherwise distribute them, provided that attribution to the author is given, the modifica-
tions are clearly noted, and the modified copies are distributed under the same license as
the original. This includes translations to other languages.
While it is not actually required by the license, I do appreciate hearing from people who
are using or distributing my work.
∗ ∗ ∗
A technical note on production: The on-line and PDF versions of this book are created
from a single source, which is written largely in XML. To produce the PDF version, the XML
is processed into a form that can be used by the TeX typesetting program. In addition to XML
files, the source includes DTDs, XSLT transformations, Java source code files, image files, a
TeX macro file, and a couple of scripts that are used in processing. I have not made the source
materials available for download, since they are not in a clean enough form to be publishable,
and because it would require a fair amount of expertise to make any use of them. However,
they are not meant to be secret, and I am willing to make them available on request.
∗ ∗ ∗
Professor David J. Eck
Department of Mathematics and Computer Science
Hobart and William Smith Colleges
Geneva, New York 14456, USA
Email: [email protected]
WWW: http://math.hws.edu/eck/
xvi Preface
Chapter 1
When you begin a journey, it’s a good idea to have a mental map of the terrain you’ll
be passing through. The same is true for an intellectual journey, such as learning to write
computer programs. In this case, you’ll need to know the basics of what computers are and
how they work. You’ll want to have some idea of what a computer program is and how one is
created. Since you will be writing programs in the Java programming language, you’ll want to
know something about that language in particular and about the modern, networked computing
environment for which Java is designed.
As you read this chapter, don’t worry if you can’t understand everything in detail. (In fact,
it would be impossible for you to learn all the details from the brief expositions in this chapter.)
Concentrate on learning enough about the big ideas to orient yourself, in preparation for the
rest of the book. Most of what is covered in this chapter will be covered in much greater detail
later in the book.
1
2 CHAPTER 1. THE MENTAL LANDSCAPE
location. The CPU can also store information in memory by specifying the information to be
stored and the address of the location where it is to be stored.
On the level of machine language, the operation of the CPU is fairly straightforward (al-
though it is very complicated in detail). The CPU executes a program that is stored as a
sequence of machine language instructions in main memory. It does this by repeatedly reading,
or fetching , an instruction from memory and then carrying out, or executing , that instruc-
tion. This process—fetch an instruction, execute it, fetch another instruction, execute it, and so
on forever—is called the fetch-and-execute cycle. With one exception, which will be covered
in the next section, this is all that the CPU ever does.
The details of the fetch-and-execute cycle are not terribly important, but there are a few
basic things you should know. The CPU contains a few internal registers, which are small
memory units capable of holding a single number or machine language instruction. The CPU
uses one of these registers—the program counter , or PC—to keep track of where it is in the
program it is executing. The PC stores the address of the next instruction that the CPU should
execute. At the beginning of each fetch-and-execute cycle, the CPU checks the PC to see which
instruction it should fetch. During the course of the fetch-and-execute cycle, the number in the
PC is updated to indicate the instruction that is to be executed in the next cycle. (Usually,
but not always, this is just the instruction that sequentially follows the current instruction in
the program.)
∗ ∗ ∗
Memory
00101110 (Location 0)
11010011 (Location 1)
Data to memory 01010011 (Location 2)
00010000 (Location 3)
CPU 10111111
Data from memory 10100110
11101001
Program 00000111
counter: 10100110
Address for
1011100001 reading/writing 00010001
data 00111110 (Location 10)