Preface to the Third Edition
Java 8 is the new kid on the block. Java 7 was asignificant but incremental improvement over its predecessors. Somuch has changed since the previous edition of this book! What wasnew in Java 5 has become ubiquitous in Java: annotations, generictypes, concurrency utilities, and more. APIs have come and goneacross the entire tableau of Java: JavaME is pretty much dead nowthat BlackBerry has abandoned it; JSF is (slowly) replacing JSP inparts of Enterprise Java; and Spring continues to expand itsreach. Many people seem to think that desktop Java is dead oreven that Java is dying, but it is definitely not rolling overyet; Swing, JavaFX, Java Enterprise, and (despite a majorlawsuit by Oracle) Android are keeping the Java language very muchalive. Additionally, a renewed interest in other JVM languages suchas Groovy, JRuby, Jython, Scala, and Clojure is keeping the platformin the forefront of the development world.
Indeed, the main challenge in preparing this third edition has beennarrowing down the popular APIs, keeping my own excitement and biases incheck, to make a book that will fit into the size constraintsestablished by the OReilly Cookbook series and my own previouseditions. The book has to remain around 900 pages in length,and it certainly would not were I to try to fit in all that glistens.
Ive also removed certain APIs that were in the previous editions.Most notable is the chapter on serial and parallel ports (pareddown to one recipe in ); computers generally dontship with these anymore, and hardly anybody is using them: the mainattention has moved to USB, and there doesnt seem to be a standardAPI for Java yet (nor, frankly, much real interest among developers).
Preface to Previous Editions
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 learnmore. If you dont know any Java yet, you should start with oneof the more introductory books, such as Head First Java (OReilly) or Learning Java (OReilly)if youre new to this family of languages, or Java in a Nutshell (OReilly) if youre 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 of being 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, Javas 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)
So Oak became Java[] 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 OReillys Perl Cookbook , Tom Christiansen and Nathan Torkington worked out a very successful design, presenting the material in small, focused articles called recipes, for the then-new Perl language. 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). (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.
Who This Book Is For
Im going to assume that you know the basics of Java. I wont tellyou how to println
a string and a number at the same time, or howto write a class that extends JFrame
and prints your name in thewindow. Ill presume youve taken a Java course or studied anintroductory book such as covers some techniques that youmight not know very well and that are necessary to understand someof the later material. Feel free to skip around! Both the printedversion of the book and the electronic copy are heavily cross-referenced.
Whats in This Book?
Unlike my Perl colleagues Tom and Nathan, I dont have to spend as much time on the oddities and idioms of the language; Java is refreshingly free of strange quirks.[] But that doesnt mean its trivial to learn well! If it were, thered be no need for thisbook. My main approach, then, is to concentrate on the Java APIs. Ill teach you byexample what the important 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 once did in Cexcept for device drivers and legacy systemsI now do in Java.
Java is suited to a different range of tasks than Perl, however. Perl (and other scripting languages, such as awk and Python) is 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. Although Java can certainly do these things, it seems more suited to development in the large, or enterprise applications development, because it is a compiled, object-oriented language. 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 fragment of code you see here (except for some one- or two-liners) has been compiled and run.
Some of the longer examples in this book are tools that I originally wrote to automate some mundane task or another. For example, a tool called MkIndex
(in the javasrc repository) 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 (see .
Organization of This Book
Lets go over the organization of this book. I start off by describing some methods of compiling your program on different platforms, running them in different environments (browser, command line, windowed desktop), and debugging.
moves from compiling and running your program to getting it to adapt to the surrounding countrysidethe other programs that live in your computer.
The next few chapters deal with basic APIs. 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.
teaches you how to use the powerful regular expressions technology from Unix in many string-matching and pattern-matching problem domains. Regex processing has been standard in Java for years, but if you dont know how to use it, you may be reinventing the flat tire.