What's in This Book?
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 the XmlForm program. This program also handles by use of another program, GetMark full and partial code insertions from the source directory. XmlForm is 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 int and double, as well as the corresponding API classes (Integer, 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 topic.
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 java.util package.
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 methods of 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.