Back to TOC Book Review


Hooked on Java

reviewed by Douglas A. Nickerson


Title: Hooked On Java
Authors: Arthur van Hoff,
Sami Shaio, Orca Starbuck
Publisher: Addison-Wesley Developers Press
Pages: 181
Price: 29.95 (US)
ISBN: 0-201-48837-X

Late last year, I began to hear about Java, in the press and from programmers I know. I was excited not because of the opportunity to develop a Web site, but because I heard Java is a general purpose language which "improves upon'' C. I bought Hooked on Java because it came with a development system (on CD-ROM). The only other book available in February 1996, Learn Java in 21 Days, looked trendy. The authors of Hooked, members of the original Java team, also gave it some credibility.

Chapter Outline

The first two chapters, "Introducing Java and Java Applets'' and "Java and the Internet,'' make a stab at explaining what Java is, what object-oriented software is, along with the history of the language. Chapter 3, "Applets Explained,'' is a short introduction to its topic, and Chapter 4, "Cool Applets,'' is a cookbook or catalog of the various applets provided on the CD. Each applet is accompanied by a description and a good quality black-and-white screen shot. How to configure the applet in a Web page and initialize its parameters is described. The book comes with the Java compiler (javac.exe), the applets in runnable and source form, ample documentation in HTML format, and the Applet Viewer. The latter is necessary if you don't have a Java-compatible Web browser.

For programmers, especially those with C experience, Chapters 5 and 6 are of the most interest. Chapter 5, "Java in Depth,'' doesn't pretend to be an exhaustive account of Java, but rather background information to get you started. It mostly succeeds. Java syntax is summarized: Basic types, arrays, expressions, control-flow, classes and interfaces, methods and fields, and exceptions. I like the organization of the material here. The sections are short, the descriptions seem concise and accurate without being overly formal. Reference is made to parallel C syntax where Java is similar or departs from C. For example, Java arrays cannot be allocated on the stack, and basic types all have fixed sizes regardless of platform.

Chapter 5 switches to a higher gear with the following sections: Objects and Classes, Class Inheritance, Interfaces, Packages, Exceptions, Threads, Monitors, and Synchronization. I'll summarize only those sections that were interesting or new.

Objects and Classes -- Classes are introduced with a simple class for Rectangle.

Inheritance -- Inheritance is introduced using the ubiquitous Shape example -- a generic Shape class with derived classes of Circle and Rectangle. This example may be well worn, but here it is done as well as I've seen it. Method overriding (there is no operator overriding in Java) is discussed without once mentioning the word virtual. All derived methods in Java are virtual. If they have the same name and arguments as a parent method, the "right'' method will always be called. No more omitting the virtual keyword by mistake.

A short digression on types elucidates the mystery of how a Circle object can be assigned to a Shape pointer (really a reference in Java) at run time. Casting is not mentioned, because it doesn't have to be. Bruce Eckel, in his book C++ Inside and Out, offers an almost identical passage in his section on virtual functions [1], which requires a discussion of upcasts and downcasts.

Interfaces -- Interfaces are new, but not really. As far as I can tell, interfaces are Java's answer to both abstract classes and multiple inheritance. Interfaces can't be instantiated directly, so they rely on their subclasses to implement their behavior, using the implements keyword. A single class can implement more than one interface. (There is no multiple inheritance for regular classes.)

Threads -- Parallel processing is accomplished in Java via threads. A thread is a sequence of execution with all its data, called a context. The Java Thread class has methods to start, suspend, and stop a Thread. Chapter 6 has a template program declaring an animator class that can be run as a thread. More on this later.

Packages -- Packages are a way to group a bunch of related classes together, thus providing a unit of modularity larger than a single class. Much of the Java runtime is made up of various packages, all with the prefix java (java.io is the input/output package). A Java programmer "imports'' or includes a package with a wild-card notation -- import java.io.* makes the input/output package available in a single source file. With import, the programmer doesn't worry about multiple inclusion of symbols, but the book does not explain why that is so.

Chapter 6, "Building an Applet,'' is really the heart of the book. It covers building an applet and integrating it into a Web page. The first hard look at the runtime, the Applet class is described in fair detail. An in-depth, but of course not exhaustive, examination of the Abstract Window Toolkit (AWT) is next. This was easier to grasp than your average application framework, which can be immense. A GUI Toolkit that is platform independent, the AWT provides graphics (lines, rectangles, etc.) and user interface controls (buttons, text fields). Here I found myself wanting to try things out in a way I haven't since fooling around with Lisp.

The other interesting thing in this chapter is the animation template example, which demonstrates animation, threads, and using the Runnable interface all at the same time.

Cool Applets

I think of an applet as a small application with limited scope, that adds some special function to a Web page. The book comes with 32 applets on the CD. Some seem immediately useful in a Web page, others are just entertaining and good examples. Some useful applets: Scrolling Images, Image Loop (a simple animation), What's New (blinking text), and Audio Item (lets you put sound into a page). Some entertaining ones: Bouncing Heads and Waving Duke. An annoying one: Nervous Text.

Software on CD

The book comes with the Java compiler (the command line executable javac.exe), the Applet Viewer, and the sample applets in source and byte-code form. It also comes with extensive documentation in HTML format. Windows 95, Windows NT, and Sun Solaris 2.x are supported.

I first tried to unzip the applets and executables while running Windows 3.1, but PKZIP choked on the long file names. After installing Windows 95, the install was successful. The Applet Viewer had trouble displaying a few of the applets, however. The trouble seemed to have to do with errors in the various files named index.html. Animator references a sound file that couldn't be loaded, Bouncing Heads couldn't find its heads (although example1.html works), and Tumbling Duke did not tumble -- until I changed the value of the 'img' parameter to "tumble.''

These glitches were all minor, and easy to fix. Basically, the HTML files were out of synch with the directory structure. An appendix lists links to various Java web sites, and the site http://www.javasoft.com/hooked/ has updates and corrections covering some of the errors mentioned above.

I did not compile all of the source code, but those applets I compiled had no errors. It can be hard to understand some of the compiler errors when compiling your own Java programs. The compiler gives a trace starting with the object that detected the error on back to the beginning of time. This is probably my ignorance more than anything else.

Audience/Overall Impression

This book attempts to appeal to "Webmasters,'' and "people who want their own home page,'' on the one hand and people "programming on purpose'' on the other. Or, maybe it tries to ride the wave of Java hype without alienating serious programmers. Whichever, it mostly succeeds. The writing and organization is clear, and appropriate for the audiences being addressed.

The number of switched words and typographical errors (I stopped counting at about five) may indicate that this book was rushed to press. I couldn't find any real inaccuracies though, except for two unclear passages in Chapter 6. One indicates that Container is "The parent class of all components.'' Another states that "The Container class is a subclass of the Component class.''

At first the lack of lengthy discussion of language features put me off. But as I began to try some of the short program examples in Chapter 6, I began to be, well, if not hooked, at least very interested. Many programmers learn best by examples, and Chapter 6 has some good ones. Like a series of progressive exercises on the piano, they build gradually upon what has gone before. I was able to load an image (a .GIF file) from disk, and display it with not too much trouble. Using the animation template example, which uses threads, I then animated myself waving from the Applet Viewer. This was all possible by copying short sections of code, changing what I needed to, and compiling them with the Java compiler.

Hooked On Java is a "try it out'' book. As such, it would be much less useful without the development environment. This book probably won't end up being your main reference work on Java (although the CD documentation is extensive). But even if you don't know or care about applets, they offer a self-contained environment with known boundaries for learning. And, since they offer immediate feedback with sound and pictures, using this book can be fun too.

I came to this book looking for the next language that would give me the kick I got from C. I 'm not entirely a convert, but I am a lot more enthusiastic. In the long run, that may be more important than a serious tome. o

References

[1] Bruce Eckel. C++ Inside and Out. (Osborne McGraw-Hill Publishing), 1993, p. 419.

Doug Nickerson has a B.S in Computer Science from the University of Massachusetts., Amherst. He works at Onset Computer Corporation, in Pocasset, MA., a maker of low-cost, low-power, data loggers. He has programed in C/C++, the Windows API, and used the OWL and Zinc frameworks in C++. He may be reached at doug_nickerson@onsetcomp.com.