CH 00
CH 00
CH 00
This book is a desktop quick reference for Java™ programmers, designed to sit
faithfully by your keyboard while you program. Part I of the book is a fast-paced,
“no-fluff” introduction to the Java programming language and the core APIs of the
Java platform. Part II is a quick-reference section that succinctly details most
classes and interfaces of those core APIs. The book covers Java 1.0, 1.1, 1.2, 1.3,
and 1.4.
Assert statement
The Java language has been extended to support assertions with the assert
statement. This new statement is documented in Chapter 2.
JavaBeans persistence
JavaBeans and related objects can now be serialized to XML documents. See
java.beans.XMLEncoder in Chapter 9 for more information.
xiii
You’ll find examples illustrating how to use most of these new APIs in Chapter 4.
In addition to all the new content, there have been a few organizational changes
to the book. In previous editions, the quick reference was organized with one
package to a chapter. This edition documents 46 distinct packages, which would
make for an excessive number of chapters. In this edition, therefore, related pack-
ages (those with a common prefix) are grouped into a single chapter, shortening
the quick reference to a more manageable 15 chapters. Because the quick refer-
ence has a purely alphabetical organization, however, the chapter boundaries are
largely irrelevant, and you can find what you need simply by flipping through the
quick reference as you would flip through a dictionary or phone book.
Another change caused by the dramatically increased number of packages is that I
was forced to cut the package hierarchy figures that appeared at the start of each
chapter in previous editions. These figures were all carefully hand-drawn and have
become an increasingly large burden on the technical illustration staff at O’Reilly &
Associates, Inc. Furthermore, the figures simply haven’t proven to be as useful as
they once seemed. In this edition, I decided that the figures’ benefit simply didn’t
justify their cost. If you are one of the minority of readers who was fond of those
diagrams, I apologize for their removal.
xiv Preface
Chapter 1: Introduction
This chapter is an overview of the Java language and the Java platform that
explains the important features and benefits of Java. It concludes with an
example Java program and walks the new Java programmer through it line by
line.
Chapter 2: Java Syntax From the Ground Up
This chapter explains the details of the Java programming language. It is a
long and detailed chapter. Experienced Java programmers can use it as a lan-
guage reference. Programmers with substantial experience with languages
such as C and C++ should be able to pick up Java syntax by reading this
chapter. The chapter does not assume years of programming experience,
however, and does not even require familiarity with C or C++. Even beginning
programmers, with only a modest amount of experience should be able to
learn Java programming by studying this chapter carefully.
Chapter 3: Object-Oriented Programming in Java
This chapter describes how the basic Java syntax documented in Chapter 2 is
used to write object-oriented programs in Java. The chapter assumes no prior
experience with OO programming. It can be used as a tutorial by new pro-
grammers or as a reference by experienced Java programmers.
Chapter 4: The Java Platform
This chapter is an overview of the essential Java APIs covered in this book. It
contains numerous short examples that demonstrate how to perform common
tasks with the classes and interfaces that comprise the Java platform. Program-
mers who are new to Java, and especially those who learn best by example,
should find this a valuable chapter.
Chapter 5: Java Security
This chapter explains the Java security architecture that allows untrusted code
to run in a secure environment from which it cannot do any malicious dam-
age to the host system. It is important for all Java programmers to have at
least a passing familiarity with Java security mechanisms.
Pr eface xv
Related Books
O’Reilly publishes an entire series of books on Java programming, including sev-
eral companion books to this one. The companion books are:
xvi Preface
Pr eface xvii
Examples Online
The examples in this book are available online and can be downloaded from the
home page for the book at http://www.or eilly.com/catalog/javanut4. You also may
want to visit this site to see if any important notes or errata about the book have
been published there.
Italic
Used for emphasis and to signify the first use of a term. Italic is also used for
commands, email addresses, web sites, FTP sites, file and directory names,
and newsgroups.
Bold
Occasionally used to refer to particular keys on a computer keyboard or to
portions of a user interface, such as the Back button or the Options menu.
Constant Width
Used in all Java code and generally for anything that you would type literally
when programming, including keywords, data types, constants, method
names, variables, class names, and interface names.
Constant Width Italic
Used for the names of function arguments and generally as a placeholder to
indicate an item that should be replaced with an actual value in your
program.
Franklin Gothic Book Condensed
Used for the Java class synopses in the quick-reference section. This very nar-
row font allows us to fit a lot of information on the page without a lot of dis-
tracting line breaks. This font is also used for code entities in the descriptions
in the quick-reference section.
Franklin Gothic Demi Condensed
Used for highlighting class, method, field, property, and constructor names in
the quick-reference section, which makes it easier to scan the class synopses.
Franklin Gothic Book Condensed Italic
Used for method parameter names and comments in the quick-reference
section.
xviii Preface
http://www.or eilly.com/catalog/javanut4/
To ask technical questions or comment on this book, send email to:
bookquestions@or eilly.com
For more information about books, conferences, Resource Centers, and the
O’Reilly Network, see the O’Reilly web site at:
http://www.or eilly.com/
Pr eface xix
Acknowledgments
Many people helped in the creation of this book, and I am grateful to them all. I
am indebted to the many, many readers of the first three editions who wrote in
with comments, suggestions, bug reports, and praise. Their many small contribu-
tions are scattered throughout the book. Also, my apologies to those who made
the many good suggestions that could not be incorporated into this edition.
Paula Ferguson, a friend and colleague, was the editor of the first three editions of
this book. Her careful reading and always-practical suggestions have made the
book stronger, clearer, and more useful. Paula’s editorial duties have moved her
away from Java books and into Web programming books, and this fourth edition
was edited by Bob Eckstein, a careful editor with a great sense of humor.
The new material I wrote for this edition has been reviewed by a number of engi-
neers at Sun, and often these engineers were the very ones who created or
worked on the APIs for which they were reviewers. I am fortunate to have been
able to go “straight to the source” for these reviews, and am very grateful to these
engineers, who made time in their very busy schedules to read and comment on
my drafts. In alphabetical order, the reviewers were:
• Josh Bloch, author of the excellent book Effective Java Programming Lan-
guage Guide, reviewed the new material on assertions and the Preferences
API.
• Graham Hamilton reviewed the Logging API material.
• Jonathan Knudsen (who is also an O’Reilly author) reviewed the JSSE and
Certification Path material.
• Charlie Lai reviewed the JAAS material.
• Ram Marti reviewed the JGSS material.
• Philip Milne, a former Sun employee, now at Dresdner Kleinwort Wasserstein,
reviewed the material on the Java Beans persistence mechanism.
• Mark Reinhold reviewed the java.nio material. Mark deserves special thanks
for having been a reviewer for the second, third, and fourth editions of this
book.
• Andreas Sterbenz and Brad Wetmore reviewed the JSSE material.
In addition to these reviewers from Sun, Ron Hitchens reviewed my New I/O
material, and my editor, Bob Eckstein, did double duty as the technical reviewer
for the XML material. My sincere thanks to each of these gentlemen for their care-
ful work. Any mistakes that remain in this book are, of course, my own.
xx Preface
David Flanagan
http://www.davidflanagan.com/
January 2002
Pr eface xxi