JDK 1.5, code-named Tiger, is an exciting change to the Java landscape. It introduces several major new facilities, such as generic types for better data structuring, metadata for annotating Java© classes in a flexible but well-defined manner, new pattern-based mechanisms for reading data, and a new mechanism for formatted printing. In addition, a much larger number of smaller but important changes add up to a new release that is a must for Java developers. It will be quite some time before these mechanisms are fully understood and in wide circulation, but you will want to know about them right away.
I wrote in the Afterword to the first edition that “writing this book has been a humbling experience.” I should add that maintaining it has been humbling, too. While many reviewers and writers have been lavish with their praise—one very kind reviewer called it “arguably the best book ever written on the Java programming language”—I have been humbled by the number of errors and omissions in the first edition. In preparing this edition, I have endeavored to correct these.
At the same time I have added a number of new recipes and removed a smaller number of old ones. The largest single addition is Chapter 8, which covers generic types and enumerations, features that provide increased flexibility for containers such as Java Collections. Now that Java includes a regular expressions API, Chapter 4 has been converted from the Apache Regular Expressions API to JDK 1.4 Regular Expressions.
I have somewhat hesitantly removed the chapter on Network Web, including the JabaDot Web Portal Site program. This was the longest single program example in the book, and it was showing signs of needing considerable refactoring (in fact, it needed a complete rewrite). In writing such a web site today, one would make much greater use of JSP tags, and almost certainly use a web site framework such as Struts (http://jakarta.apache.org/struts), SOFIA (http://www.salmonllc.com/), or the Spring Framework (http://www.springframework.org/) to eliminate a lot of the tedious coding. Or, you might use an existing package such as the Java Lobby’s JLCP. Material on Servlets and JavaServer pages can be found in O’Reilly’s Java Servlet & JSP Cookbook by Bruce W. Perry. Information on Struts itself can be found in Chuck Cavaness’s Programming Jakarta Struts (O’Reilly). Information on SOAP-based web services is included in O’Reilly’s Java Web Services by Dave Chappell and Tyler Jewell, so this topic is not covered here.
While I’ve tested the examples on a variety of systems and provide Ant scripts to rebuild everything, I did most of the new development and writing for this edition using Mac OS X, which truly is “Unix for the masses,” and which provides one of the best-supported out-of-the-box Java experiences. Mac OS X Java does, however, suffer a little from “new version lag” and, since 1.5 was not available for the Mac by the time this edition went to press, the JDK 1.5 material was developed and tested on Linux and Windows.
I wish to express my heartfelt thanks to all who sent in both comments and criticisms of the book after the first English edition was in print. Special mention must be made of one of the book’s German translators, Gisbert Selke, who read the first edition cover to cover during its translation and clarified my English. Gisbert did it all over again for the second edition and provided many code refactorings, which have made this a far better book than it would be otherwise. Going beyond the call of duty, Gisbert even contributed one recipe (Recipe 26.4) and revised some of the other recipes in the same chapter. Thank you, Gisbert! The second edition also benefited from comments by Jim Burgess, who read large parts of the book. Comments on individual chapters were received from Jonathan Fuerth, Kim Fowler, Marc Loy, and Mike McCloskey. My wife Betty and teenaged children each proofread several chapters as well.
The following people contributed significant bug reports or suggested improvements from the first edition: Rex Bosma, Rod Buchanan, John Chamberlain, Keith Goldman, Gilles-Philippe Gregoire, B. S. Hughes, Jeff Johnston, Rob Konigsberg, Tom Murtagh, Jonathan O’Connor, Mark Petrovic, Steve Reisman, Bruce X. Smith, and Patrick Wohlwend. My thanks to all of them, and my apologies to anybody I’ve missed.
My thanks to the good guys behind the O’Reilly “bookquestions” list for fielding so many questions. Thanks to Mike Loukides, Deb Cameron, and Marlowe Shaeffer for editorial and production work on the second edition.
If you know a little Java, great. If you know more Java, even better! This book is ideal for anyone who knows some Java and wants to learn more. If you don’t know any Java yet, you should start with one of the more introductory books from O’Reilly, such as Head First Java or Learning Java if you’re new to this family of languages, or Java in a Nutshell if you’re an experienced C programmer.
I started programming in C in 1980 while working at the University of Toronto, and C served me quite well through the 1980s and into the 1990s. In 1995, as the nascent language Oak was being renamed Java, I had the good fortune to be told about it by my colleague J. Greg Davidson. I sent an email to the address Greg provided, and got this mail back from James Gosling, Java’s inventor, in March 1995:
> Hi. A friend told me about WebRunner(?), your extensible network > browser. It and Oak(?) its extension language, sounded neat. Can > you please tell me if it's available for play yet, and/or if any > papers on it are available for FTP? Check out http://java.sun.com (oak got renamed to java and webrunner got renamed to hotjava to keep the lawyers happy)
I downloaded HotJava and began to play with it. At first I wasn’t sure about this newfangled language, which looked like a mangled C/C++. I wrote test and demo programs, sticking them a few at a time into a directory that I called javasrc to keep it separate from my C source (because often the programs would have the same name). And as I learned more about Java, I began to see its advantages for many kinds of work, such as the automatic memory reclaim and the elimination of pointer calculations. The javasrc directory kept growing. I wrote a Java course for Learning Tree, and the directory grew faster, reaching the point where it needed subdirectories. Even then, it became increasingly difficult to find things, and it soon became evident that some kind of documentation was needed.
In a sense, this book is the result of a high-speed collision between my javasrc directory and a documentation framework established for another newcomer language. In O’Reilly’s Perl Cookbook, Tom Christiansen and Nathan Torkington worked out a very successful design, presenting the material in small, focused articles called “recipes.” The original model for such a book is, of course, the familiar kitchen cookbook. Using the term “cookbook” to refer to an enumeration of how-to recipes relating to computers has a long history. On the software side, Donald Knuth applied the “cookbook” analogy to his book The Art of Computer Programming (Addison Wesley), first published in 1968. On the hardware side, Don Lancaster wrote The TTL Cookbook (Sams, 1974). (Transistor-transistor logic, or TTL, was the small-scale building block of electronic circuits at the time.) Tom and Nathan worked out a successful variation on this, and I recommend their book for anyone who wishes to, as they put it, “learn more Perl.” Indeed, the work you are now reading strives to be the book for the person who wishes to “learn more Java.”
The code in each recipe is intended to be largely self-contained; feel free to borrow bits and pieces of any of it for use in your own projects. The code is distributed with a Berkeley-style copyright, just to discourage wholesale reproduction.
I’m going to assume that you know the basics of Java. I won’t tell
you how to
println a string and a
number at the same time, or how to write a class that extends
Applet and prints your name in the window.
I’ll presume you’ve taken a Java course or studied an introductory book
such as O’Reilly’s Head First Java,
Learning Java, or Java in a
Nutshell. However, Chapter
1 covers some techniques that you might not know very well and
that are necessary to understand some of the later material. Feel free
to skip around! Both the printed version of the book and the electronic
copy are heavily cross-referenced.
Unlike my Perl colleagues Tom and Nathan, I don’t have to spend as much time on the oddities and idioms of the language; Java is refreshingly free of strange quirks. But that doesn’t mean it’s trivial to learn well! If it were, there’d be no need for this book. My main approach, then, is to concentrate on the Java APIs. I’ll teach you by example what the APIs are and what they are good for.
Like Perl, Java is a language that grows on you and with you. And, I confess, I use Java most of the time nowadays. Things I’d once done in C are now—except for device drivers and legacy systems—done in Java.
But Java is suited to a different range of tasks than Perl. Perl (and other scripting languages, such as awk and Python) are particularly suited to the “one-liner” utility task. As Tom and Nathan show, Perl excels at things like printing the 42nd line from a file. While it can certainly do these things, Java, because it is a compiled, object-oriented language, seems more suited to “development in the large” or enterprise applications development. Indeed, much of the API material added in Java 2 was aimed at this type of development. However, I will necessarily illustrate many techniques with shorter examples and even code fragments. Be assured that every line of code you see here has been compiled and run.
Many of the longer examples in this book are tools that I
originally wrote to automate some mundane task or another. For example,
MkIndex (described in Chapter 17) reads the top-level
directory of the place where I keep all my Java example source code and
builds a browser-friendly index.html file for that directory. For
another example, the body of the first edition was partly composed in
XML, a simplification that builds upon many years of
experience in SGML (the parent standard that led to the tag-based syntax
of HTML). It is not clear at this point if XML will primarily be useful
as a publishing format or as a data manipulation format, or if its
prevalence will further blur that distinction, although it seems that
the blurring of distinctions is more likely. However, I used XML here to
type in and mark up the original text of some of the chapters of this
book. The text was then converted to the publishing software format by
XmlForm program. This program
also handles—by use of another program,
GetMark—full and partial code insertions from
the source directory.
discussed in Chapter 21.
Let’s go over the organization of this book. I start off Chapter 1, by describing some methods of compiling your program on different platforms, running them in different environments (browser, command line, windowed desktop), and debugging. Chapter 2, moves from compiling and running your program to getting it to adapt to the surrounding countryside—the other programs that live in your computer.
The next few chapters deal with basic APIs. Chapter 3, concentrates on one of the most basic but powerful data types in Java, showing you how to assemble, dissect, compare, and rearrange what you might otherwise think of as ordinary text.
Chapter 4, teaches you how to use the powerful regular expressions technology from Unix in many string-matching and pattern-matching problem domains. JDK 1.4 was the first release to include this powerful technology; I also mention several third-party regular expression packages.
Chapter 5, deals both with
built-in types such as
double, as well as the corresponding
API classes (
Double, etc.) and the conversion and testing
facilities they offer. There is also brief mention of the “big number”
classes. Since Java programmers often need to deal in dates and times,
both locally and internationally, Chapter 6, covers this important
The next two chapters cover data processing. As in most languages,
arrays in Java are linear, indexed collections of similar-kind
objects, as discussed in Chapter
7. This chapter goes on to deal with the many “Collections”
classes: powerful ways of storing quantities of objects in the
A new chapter was added in this section of the second edition. JDK 1.5 introduced a new dimension to the notion of data structuring, by adapting the C++ notion of templates to the Java Collections; the result known as Generics is the main subject of Chapter 8.
Despite some syntactic resemblance to procedural languages such as
C, Java is at heart an object-oriented programming language. Chapter 9, discusses some of the key
notions of OOP as it applies to Java, including the commonly overridden
java.lang.Object and the
important issue of Design Patterns.
The next few chapters deal with aspects of traditional input and
output. Chapter 10, details the
rules for reading and writing files. (Don’t skip this if you think files
are boring, as you’ll need some of this information in later chapters:
you’ll read and write on serial or parallel ports in Chapter 12 and on a socket-based
network connection in Chapter
16!) Chapter 11, shows
you everything else about files—such as finding their size and
last-modified time—and about reading and modifying directories, creating
temporary files, and renaming files on disk. Chapter 12, shows how you can use the
javax.comm API to read/write on
serial and parallel ports using a standard Java API.
Chapter 13, leads us into the GUI development side of things. This chapter is a mix of the lower-level details, such as drawing graphics and setting fonts and colors, and very high-level activities, such as controlling a video clip or movie. In Chapter 14, I cover the higher-level aspects of a GUI, such as buttons, labels, menus, and the like—the GUI’s predefined components. Once you have a GUI (really, before you actually write it), you’ll want to read Chapter 15, so your programs can work as well in Akbar, Afghanistan, Algiers, Amsterdam, or Angleterre as they do in Alberta, Arkansas, or Alabama . . . .
Since Java was originally promulgated as “the programming language for the Internet,” it’s only fair that we spend some of our time on networking in Java. Chapter 16, covers the basics of network programming from the client side, focusing on sockets. We’ll then move to the server side in Chapter 17. In Chapter 18, you’ll learn more client-side techniques. Programs on the Net often need to generate electronic mail, so this section ends with Chapter 19.
Chapter 20, covers the essentials of the Java Database Connectivity (JDBC) and Java Data Objects (JDO) packages, showing how you can connect to local or remote relational databases, store and retrieve data, and find out information about query results or about the database.
Another form of storing and exchanging data is XML. Chapter 21, discusses XML’s formats and some operations you can apply using SAX and DOM, two standard Java APIs.
Chapter 22, takes the distributed notion one step further and discusses Remote Method Invocation, Java’s standard remote procedure call mechanism. RMI lets you build clients, servers, and even “callback” scenarios, using a standard Java mechanism—the Interface—to describe the contract between client and server.
Chapter 23, shows how to create packages of classes that work together. This chapter also talks about “deploying” or distributing and installing your software.
Chapter 24, tells you how to write classes that appear to do more than one thing at a time and let you take advantage of powerful multiprocessor hardware.
Chapter 25, lets you in on such secrets as how to write API cross-reference documents mechanically (“become a famous Java book author in your spare time!”) and how web browsers are able to load any old applet—never having seen that particular class before—and run it.
Sometimes you already have code written and working in another language that can do part of your work for you, or you want to use Java as part of a larger package. Chapter 26, shows you how to run an external program (compiled or script) and also interact directly with “native code” in C/C++ or other languages.
There isn’t room in an 800-page book for everything I’d like to tell you about Java. The Chapter 27 presents some closing thoughts and a link to my online summary of Java APIs that every Java developer should know about.
No two programmers or writers will agree on the best order for presenting all the Java topics. To help you find your way around, I’ve included extensive cross-references, mostly by recipe number.
Java has gone through five major versions. The first official release was JDK 1.0, and its last bug-fixed version was 1.0.2. The second major release is Java JDK 1.1, and the latest bug-fixed version is 1.1.9, though it may be up from that by the time you read this book. The third major release, in December 1998, was to be known as JDK 1.2, but somebody at Sun abruptly renamed JDK 1.2 at the time of its release to Java 2, and the implementation is known as Java 2 SDK 1.2. The current version as of the writing of the first edition of this book was Java 2 SDK 1.3 (JDK 1.3), which was released in 2000.
As the first edition of this book went to press, Java 2 Version 1.4 was about to appear; it entered beta (which Sun calls “early access”) around the time of the book’s completion so I could mention it only briefly. The second edition of this book looks to have better timing; Java 2 Version 1.5 is in beta as I am updating the book.
This book is aimed at the fifth version, Java 2 Standard Edition, Version 1.5. By the time of publication, I expect that all Java projects in development will be using JDK 1.4, with a very few wedded to earlier versions for historical reasons. I have used several platforms to test this code for portability. I’ve tested with Sun’s Linux JDK. For the mass market, I’ve tested many of the programs on Sun’s Win32 (Windows 2000/XP/2003) implementation. And, “for the rest of us,” I’ve done most of my recent development using Apple’s Mac OS X Version 10.2.x and later. However, since Java is portable, I anticipate that the vast majority of the examples will work on any Java-enabled platform, except where extra APIs are required. Not every example has been tested on every platform, but all have been tested on at least one—and most on more than one.
The Java API consists of two parts: core APIs and noncore APIs. The core is, by definition, what’s included in the JDK that you download for free from http://java.sun.com/. Noncore is everything else. But even this “core” is far from tiny: it weighs in at around 50 packages and well over 2,000 public classes, averaging around 12 public methods each. Programs that stick to this core API are reasonably assured of portability to any Java platform.
The noncore APIs are further divided into standard extensions and nonstandard extensions. All standard extensions have package names beginning with
javax. (and reference implementations are available from Sun). A
Java licensee (such as Apple or IBM) is not required to implement every
standard extension, but if it does, the interface of the standard
extension should be adhered to. This book calls your attention to any
code that depends on a standard extension. Little code here depends on
nonstandard extensions, other than code listed in the book itself. My
some utility classes used here and there; you will see an import for
this at the top of any file that uses classes from it.
In addition, two other platforms, the J2ME and the J2EE, are standardized. Java 2 Micro Edition is concerned with small devices such as handhelds (PalmOS and others), cell phones, fax machines, and the like. Within J2ME are various “profiles” for different classes of devices. At the high end, the Java 2 Enterprise Edition (J2EE) is concerned with building large, scalable, distributed applications. Servlets, JavaServer Pages, JavaServer Faces, CORBA, RMI, JavaMail, Enterprise JavaBeans© (EJBs), Transactions, and other APIs are part of the J2EE. J2ME and J2EE packages normally begin with “javax” as they are not core J2SE packages. This book does not cover J2ME at all but includes a few of the J2EE APIs that are also useful on the client side, such as RMI and JavaMail. As mentioned earlier, coverage of Servlets and JSPs from the first edition of this book has been removed as there is now a S ervlet and JSP Cookbook.
A lot of useful information is packed into this book. However, due to the breadth of topics, it is not possible to give book-length treatment to any one topic. Because of this, the book also contains references to many web sites and other books. This is in keeping with my target audience: the person who wants to learn more about Java.
O’Reilly publishes, in my opinion, the best selection of Java books on the market. As the API continues to expand, so does the coverage. You can find the latest versions and ordering information on O’Reilly’s Java books online at http://java.oreilly.com, and you can buy them at most bookstores, both physical and virtual. You can also read them online through a paid subscription service; see http://safari.oreilly.com. While many are mentioned at appropriate spots in the book, a few deserve special mention here.
First and foremost, David Flanagan’s Java in a Nutshell offers a brief overview of the language and API and a detailed reference to the most essential packages. This is handy to keep beside your computer. Head First Java offers a much more whimsical introduction to the language and is recommended for the less experienced developer.
A definitive (and monumental) description of programming the Swing GUI is Java Swing by Marc Loy, Robert Eckstein, Dave Wood, James Elliott, and Brian Cole.
Java Virtual Machine, by Jon Meyer and Troy Downing, will intrigue the person who wants to know more about what’s under the hood. This book is out of print but can be found used and in libraries.
Java Network Programming and Java I/O, both by Elliotte Rusty Harold, and Database Programming with JDBC and Java, by George Reese, are also useful references.
There are many more; see the O’Reilly web site for an up-to-date list.
You should not consider releasing a GUI application unless you have read Sun’s official Java Look and Feel Design Guidelines (Addison Wesley). This work presents the views of a large group of human factors and user-interface experts at Sun who have worked with the Swing GUI package since its inception; they tell you how to make it work well.
Donald E. Knuth’s The Art of Computer Programming has been a source of inspiration to generations of computing students since its first publication by Addison Wesley in 1968. Volume 1 covers Fundamental Algorithms, Volume 2 is Seminumerical Algorithms, and Volume 3 is Sorting and Searching. The remaining four volumes in the projected series are still not completed. Although his examples are far from Java (he invented a hypothetical assembly language for his examples), many of his discussions of algorithms—of how computers ought to be used to solve real problems—are as relevant today as they were years ago.
Though somewhat dated now, the book The Elements of Programming Style, by Kernighan and Plauger, set the style (literally) for a generation of programmers with examples from various structured programming languages. Kernighan and Plauger also wrote a pair of books, Software Tools and Software Tools in Pascal, which demonstrated so much good advice on programming that I used to advise all programmers to read them. However, these three books are dated now; many times I wanted to write a follow-on book in a more modern language, but instead defer to The Practice of Programming, Brian’s follow-on—co-written with Rob Pike—to the Software Tools series. This book continues the Bell Labs (now part of Lucent) tradition of excellence in software textbooks. In Recipe 3.13, I have even adapted one bit of code from their book.
See also The Pragmatic Programmer by Andrew Hunt and David Thomas (Addison Wesley).
Peter Coad’s Java Design (PTR-PH/Yourdon Press) discusses the issues of object-oriented analysis and design specifically for Java. Coad is somewhat critical of Java’s implementation of the observable-observer paradigm and offers his own replacement for it.
One of the most famous books on object-oriented design in recent years is Design Patterns, by Gamma, Helm, Johnson, and Vlissides (Addison Wesley). These authors are often collectively called “the gang of four,” resulting in their book sometimes being referred to as “the GOF book.” One of my colleagues called it “the best book on object-oriented design ever,” and I agree; at the very least it’s among the best.
Refactoring, by Martin Fowler, covers a lot of " coding cleanups” that can be applied to code to improve readability and maintainability. Just as the GOF book introduced new terminology that helps developers and others communicate about how code is to be designed, Fowler’s book provided a vocabulary for discussing how it is to be improved. Many of the “refactorings” now appear in the Refactoring Menu of the Eclipse IDE (see Recipe 1.3).
Two important streams of methodology theories are currently in circulation. The first is collectively known as Agile Methods, and its best-known member is Extreme Programming. XP (the methodology, not last year’s flavor of Microsoft’s OS) is presented in a series of small, short, readable texts led by its designer, Kent Beck. A good overview of all the Agile methods is Highsmith’s Agile Software Development Ecosystems. The first book in the XP series is Extreme Programming Explained.
Another group of important books on methodology, covering the more traditional object-oriented design, is the UML series led by “the Three Amigos” (Booch, Jacobson, and Rumbaugh). Their major works are the UML User Guide, UML Process, and others. A smaller and more approachable book in the same series is Martin Fowler’s UML Distilled.
This book uses the following conventions.
I use the following terminology in this book. A program means either an applet, a servlet, or an application. An applet is for use in a browser. A servlet is similar to an applet but for use in a server. An application is any other type of program. A desktop application (a.k.a. client) interacts with the user. A server program deals with a client indirectly, usually via a network connection.
The examples shown are in two varieties. Those that begin with zero or more import statements, a Javadoc comment, and a public class statement are complete examples. Those that begin with a declaration or executable statement, of course, are excerpts. However, the full versions of these excerpts have been compiled and run, and the online source includes the full versions.
The following typographic conventions are used in this book:
Used for commands, filenames, and example URLs. It is also used to define new terms when they first appear in the text.
Used in code examples to show partial or complete Java source code program listings. It is also used for class names, method names, variable names, and other fragments of Java code.
Constant width bold
Used for user input, such as commands that you type on the command line.
Many programs are accompanied by an example showing them in
action, run from the command line. These will usually show a
prompt ending in either
$ for Unix or
> for Windows, depending on which
computer I was using that day. Text before this prompt character can
be ignored; it will be a pathname or a hostname, again depending on
As mentioned earlier, I’ve tested all the code on at least one of the reference platforms, and most on several. Still, there may be platform dependencies, or even bugs, in my code or in some important Java implementation. Please report any errors you find, as well as your suggestions for future editions, by writing to:
|O’Reilly Media, Inc.|
|1005 Gravenstein Highway North|
|Sebastopol, CA 95472|
|(800) 998-9938 (in the United States or Canada)|
|(707) 829-0515 (international or local)|
|(707) 829-0104 (fax)|
To ask technical questions or comment on the book, send email to:
I also have a personal web site for the book:
Both sites list errata and plans for future editions. You’ll also find the source code for all the Java code examples to download; please don’t waste your time typing them again! For specific instructions, see the next section.
From my web site http://javacook.darwinsys.com, just follow the Downloads link. You are presented with three choices:
Download the entire source archive as a single large zip file.
Download individual source files, indexed alphabetically as well as by chapter.
Download the binary JAR file for the
com.darwinsys.* package needed to compile
many of the other programs.
Most people will choose either option 1 or 2, but anyone who wants to compile my code will need option 3. See Recipe 1.5 for information on using these files.
Downloading the entire source archive yields a large zip file with all the files from the book (and more). This archive can be unpacked with jar (see Recipe 23.4), the free zip program from Info-ZIP, the commercial WinZip or PKZIP, or any compatible tool. The files are organized into subdirectories by topic, with one for strings (Chapter 3), regular expressions (Chapter 4), numbers (Chapter 5), and so on. The archive also contains the index by name and index by chapter files from the download site, so you can easily find the files you need.
Downloading individual files is easy, too: simply follow the links either by file/subdirectory name or by chapter. Once you see the file you want in your browser, use File → Save or the equivalent, or just copy and paste it from the browser into an editor or IDE.
The files are updated periodically, so if there are differences between what’s printed in the book and what you get, be glad, for you’ll have received the benefit of hindsight.
My life has been touched many times by the flow of the fates bringing me into contact with the right person to show me the right thing at the right time. Steve Munroe, with whom I’ve long since lost touch, introduced me to computers—in particular an IBM 360/30 at the Toronto Board of Education that was bigger than a living room, had 32 or 64K of memory, and had perhaps the power of a PC/XT—in 1970. Herb Kugel took me under his wing at the University of Toronto while I was learning about the larger IBM mainframes that came later. Terry Wood and Dennis Smith at the University of Toronto introduced me to mini- and micro-computers before there was an IBM PC. On evenings and weekends, the Toronto Business Club of Toastmasters International (http://www.toastmasters.org) and Al Lambert’s Canada SCUBA School allowed me to develop my public speaking and instructional abilities. Several people at the University of Toronto, but especially Geoffrey Collyer, taught me the features and benefits of the Unix operating system at a time when I was ready to learn it.
Greg Davidson of UCSD taught the first Learning Tree course I attended and welcomed me as a Learning Tree instructor. Years later, when the Oak language was about to be released on Sun’s web site, Greg encouraged me to write to James Gosling and find out about it. James’s reply of March 29th, 1995, that the lawyers had made them rename the language to Java and that it was “just now” available for download, is the prized first entry in my saved Java mailbox. Mike Rozek took me on as a Learning Tree course author for a Unix course and two Java courses. After Mike’s departure from the company, Francesco Zamboni, Julane Marx, and Jennifer Urick in turn provided product management of these courses. Jennifer also arranged permission for me to “reuse some code” in this book that had previously been used in my Java course notes. Finally, thanks to the many Learning Tree instructors and students who showed me ways of improving my presentations. I still teach for “The Tree” and recommend their courses for the busy developer who wants to zero in on one topic in detail over four days. Their web site is http://www.learningtree.com.
Closer to this project, Tim O’Reilly believed in “the little Lint book” when it was just a sample chapter, enabling my early entry into the circle of O’Reilly authors. Years later, Mike Loukides encouraged me to keep trying to find a Java book idea that both he and I could work with. And he stuck by me when I kept falling behind the deadlines. Mike also read the entire manuscript and made many sensible comments, some of which brought flights of fancy down to earth. Jessamyn Read turned many faxed and emailed scratchings of dubious legibility into the quality illustrations you see in this book. And many, many other talented people at O’Reilly helped put this book into the form in which you now see it.
I also must thank my first-rate reviewers for the first edition, first and foremost my dear wife Betty Cerar, who still knows more about the caffeinated beverage that I drink while programming than the programming language I use, but whose passion for clear expression and correct grammar has benefited so much of my writing during our life together. Jonathan Knudsen, Andy Oram, and David Flanagan commented on the outline when it was little more than a list of chapters and recipes, and yet were able to see the kind of book it could become, and to suggest ways to make it better. Learning Tree instructor Jim Burgess read most of the first edition with a very critical eye on locution, formulation, and code. Bil Lewis and Mike Slinn (email@example.com) made helpful comments on multiple drafts of the book. Ron Hitchens (firstname.lastname@example.org) and Marc Loy carefully read the entire final draft of the first edition. I am grateful to Mike Loukides for his encouragement and support throughout the process. Editor Sue Miller helped shepherd the manuscript through the somewhat energetic final phases of production. Sarah Slocombe read the XML chapter in its entirety and made many lucid suggestions; unfortunately time did not permit me to include all of them in the first edition. Each of these people made this book better in many ways, particularly by suggesting additional recipes or revising existing ones. The faults that remain are my own.
I used a variety of tools and operating systems in preparing, compiling, and testing the first edition. The developers of OpenBSD (http://www.openbsd.org), “the proactively secure Unix-like system,” deserve thanks for making a stable and secure Unix clone that is also closer to traditional Unix than other freeware systems. I used the vi editor (vi on OpenBSD and vim on Windows) while inputting the original manuscript in XML, and Adobe FrameMaker to format the documents. Each of these is an excellent tool in its own way, but I must add a caveat about FrameMaker. Adobe had four years from the release of OS X until I started this book revision cycle during which they could have produced a current Macintosh version of FrameMaker. They did not do so, requiring me to do the revision in the increasingly ancient Classic environment. Strangely enough, their Mac sales of FrameMaker dropped steadily during this period, until, during the final production of this book, Adobe officially announced that it would no longer be producing any Macintosh versions of this excellent publishing software, ever.
No book on Java would be complete without a quadrium of thanks to James Gosling for inventing the first Unix Emacs, the sc spreadsheet, the NeWS window system, and Java. Thanks also to his employer Sun Microsystems (NASDAQ SUNW) for creating not only the Java language but an incredible array of Java tools and API libraries freely available over the Internet.
Thanks to Tom and Nathan for the Perl Cookbook. Without them I might never have come up with the format for this book.
Willi Powell of Apple Canada provided Mac OS X access in the early days of OS X; I currently have an Apple notebook of my own. Thanks also to Apple for basing OS X on BSD Unix, making Apple the world’s largest-volume commercial Unix company.
Thanks to the Tim Horton’s Donuts in Bolton, Ontario for great coffee and for not enforcing the 20-minute table limit on the geek with the computer.
To each and every one of you, my sincere thanks.
 The first edition is available today in English, German, French, Polish, Russian, Korean, Traditional Chinese, and Simplified Chinese. My thanks to all the translators for their efforts in making the book available to a wider audience.
 Note that not all packages named
javax. are extensions:
javax.swing and its subpackages—the Swing
GUI packages—used to be extensions, but are now core.
 With apologies for algorithm decisions that are less relevant today given the massive changes in computing power now available.
 It’s a good thing he only invented four major technologies, not five, or I’d have to rephrase that to avoid infringing on an Intel trademark.