About This Book
This book teaches the Java programming language to people who are familiar with basic programming
concepts. It explains the language without being arduously formal or complete. This book is not an
introduction to object-oriented programming, although some issues are covered to establish a common
terminology. Other books in this series and much online documentation focus on applets, graphical
interfaces, Web sites, databases, components, and other specific kinds of programming tasks. For other
references, see "
Further Reading" on page 755.
This fourth edition provides integrated coverage of the Java programming language as provided by the
Java
™
2 Platform Standard Edition 5.0 and specified by the Java
™
Language Specification, Third Edition.
It also covers most of the classes in the main packages (
java.lang
,
java.util
,
java.io
) as implemented
in the
J2SE
™
Development Kit 5.0 (more commonly known as
JDK
5.0, or in the older nomenclature
JDK
1.5.0).
If you have already read the third edition of this book, you will find some major changes, both in the
language and the book, since the 1.3 release that the third edition covered. There are new chapters on
generics, enums, and annotationsthe major new language features introduced in the 5.0 releaseand
major new sections on assertions and regular expressions. Some existing material has been restructured
to accommodate other changes and to improve the general flow of the textsuch as introducing the new
boxing and unboxing conversions. But every single chapter has been updated in some way, whether it is
a new language feature like variable argument methods; the new enhanced
for
loop construct; a new
class such as
Formatter
for formatting text output; or changes to classes and methods caused by the
addition of generics (such as the collections utilities and the reflection classes)change permeates this
entire fourth edition.
The Java programming language shares many features common to most programming languages in use
today. The language should look familiar to C and C++ programmers because it was designed with C
and C++ constructs where the languages are similar. That said, this book is neither a comparative
analysis nor a "bridge" tutorialno knowledge of C or C++ is assumed. C++ programmers, especially,
may be as hindered by what they must unlearn as they are helped by their knowledge.
Chapter 1A Quick Tourgives a quick overview of the language. Programmers who are unfamiliar with
object-oriented programming notions should read the quick tour, while programmers who are already
familiar with object-oriented programming paradigms will find the quick tour a useful introduction to the
object-oriented features of the language. The quick tour introduces some of the basic language features
on which examples through the rest of the book are built.
Chapters 2 through 6 cover the object-oriented core features of the language, namely, class
declarations that define components of a program, and objects manufactured according to class
definitions. Chapter 2Classes and Objectsdescribes the basis of the language: classes. Chapter
3Extending Classesdescribes how an existing class can be extended, or subclassed, to create a new class
with additional data and behavior.
Chapter 4Interfacesdescribes how to declare interface types that are
abstract descriptions of behavior that provide maximum flexibility for class designers and implementors.
Chapter 5Nested Classes and Interfacesdescribes how classes and interfaces can be declared inside
other classes and interfaces, and the benefits that provides. Finally,
Chapter 6Enumeration Typescovers
the definition and use of type-safe enumeration constants.
Chapters 7 through 10 cover standard constructs common to most languages. Chapter 7Tokens, Values,
and Variablesdescribes the tokens of the language from which statements are constructed, the types
defined by the language and their allowed values, and the variables that store data in objects, arrays, or
locally within methods. Chapter 8Primitives as Typesexplores the relationship between the primitive