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 1) 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 book itself was partly composed in XML, a recent simplification that builds upon a decade 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, though 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 FrameMaker input 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. This is the first chapter that covers a non-standard API -- there is not yet a regular expression API in standard Java -- so I talk about several 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. Additional data structuring and programming tips appear in Chapter 8.

The next few chapters deal with aspects of traditional input and output. Chapter 9 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 11 and on a socket-based network connection in Chapter 15!) Chapter 10 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 11 shows how you can use the javax.comm API to read/write on serial and parallel ports without resorting to coding in C.

Chapter 12 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 playing video clip or movie. Then, in Chapter 13 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 14 so your programs can work as well in Akbar, Afghanistan, Algiers, Amsterdam, or Angleterre as they do in Alberta or 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 15, covers the basics of network programming from the client side, focusing on sockets. We’ll then move to the server side in Chapter 16. In Chapter 17, you’ll learn more client-side techniques. Some specialized server-side techniques for the Web are covered in Chapter 18. Finally, programs on the Net often need to generate electronic mail, so this section ends with Chapter 19.

Chapter 20 covers the Java Database Connectivity package (JDBC), 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 Methods 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 big secrets as how to write API cross reference documents mechanically 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++.

There isn’t room in an 800-page book for everything I’d like to tell you about Java. The Chapter III 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, there are extensive cross-references, mostly by recipe number.

Get Java Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.