The Codesmith's Library
Written by Carsten Whimster
In The Codesmith's Library, I focus on development books and materials. I write this column from the point of view of an intermediate programmer. Pick up whichever book strikes your fancy, and join the growing group of people following our programming columns.
Please send me your comments and thoughts so that I can make this column what you want it to be. I read and respond to all mail.
Java in a Nutshell
This particular book has arrived at my desk with good recommendations, so I thought I would take a look at what it has to offer over the roughly one million other me-too books which have recently been published about this new Internet-enabled language. Here are the chapter titles:
Part I: Introducing Java 1. Getting Started with Java 2. How Java Differs from C 3. Classes and Objects in Java Part II: Programming with the Java API 4. Applets 5. Graphical User Interfaces 6. Input and Output 7. Networking 8. Advanced Graphics and Images 9. Advanced Threads Part III: Java Language Reference 10. Java Syntax 11. Events 12. Fonts, Colors, and Cursors 13. System Properties and Applet Parameters 14. Applet Security 15. Java-Related HTML and HTTP Syntax 16. The Unicode Standard 17. JDK Development Tools Part IV: API Quick Reference 18. The java.applet Package 19. The java.awt Package 20. The java.awt.image Package 21. The java.awt.peer Package 22. The java.io Package 23. The java.lang Package 24. The java.net Package 25. The java.util Package 26. Java Errors and Exceptions Part V: API Cross References 27. Class Defined-In Index 28. Method Defined-In Index 29. Subclass Index 30. Implemented-By Index 31. Returned-By Index 32. Passed-To Index 33. Thrown-By Index
As you can see, this table of contents is quite extensive. This book really aims at being the only day-to-day reference you will need for Java, once you have learned the language, and it really shows. However, early in the book, this statement is made: "If you can't figure out how to use a method from the information given in this book, read about the method in the online documentation." Heed this warning. This book only lists the API syntax, and doesn't explain what each class is for, or how to use it. It is a quick reference, not a full reference. If you are looking to learn Java, make this your second book. The first should teach you Java, and explain the API. This one will be used forever after that.
The first part merely introduces Java, and you will likely skim it once, and never use it again. It is similar to The C Programming Language in scope and approach, but not quite as in-depth. First, we are treated to a feature-by-feature review of Java's concepts, and then walked through an in-depth examination of important differences between C and Java. Although C++ might seem closer to Java at a quick glance, Java's feel is much more like C. The simplicity of C, the carved out of a single chunk of granite feel of C, and the balance of C are all matched by Java. Java also benefits from roughly 25 years of hindsight with respect to C, and uses this to clean up each of C's mis-features, ie. the ones which are responsible for most of the bugs. Some people will claim that Java removes important features, but even programmers like myself who love C will admit that most of the crud is gone, and that the few features we will miss are well worth the newfound robustness of the new language.
The are two things which I miss in Java, especially since I have been spending time with Modula-3: proper enumerations (or any enumerations!), and sets. Both of these are huge time-savers, and should be in the language for efficiency, as far as I am concerned, but they are not here.
Java is, of course, a pure object-oriented language, and as such will not allow you to program as if you were still using C, unlike C++. I consider this to be a strength in Java, and a weakness in C++, but others will disagree. The intent is to get all the advantages of OOP, without the possiblity of reverting to old habits. The fresh feel, and the solidness of the syntax is very much like C, but unlike C++'s heavy and cumbersome inconsistencies.
The last chapter in part one gives a quick overview of OOP with Java. Object creation, constructors, automatic garbage collection and how to speed this up, class variables, class methods, the "this" pointer (really a reference in Java), initializers, and on and on are all explained here. If you already know OOP before this book, you will understand Java pretty well after this chapter. If you don't, you have some catching up to do in OOP.
At the end of this chapter, a brief comparison is made to C++, complete with explanations of why each of the features of C++ not implemented in Java were left out.
The second part is really a series of sample programs with explanations. You are again expected to be familiar with programming in general, and OOP in particular. The more unusual aspects of Java are introduced here in detail, such as applet creation and use, events, and the standalone applets, ie. applications.
This is followed by an detailed introduction to GUIs in Java. If you are used to OS/2 Presentation Manager or Windows, you will probably be surprised by how simple it is to accomplish things in Java. When programming in Windows and OS/2, I am always struck by the overwhelming tendency to overgeneralize everything past the point of usefulness, yielding clumsy APIs, structure passing everywhere, and endless initialization. Java has a much more lean feel to it, and seems to strike a good balance between focus and flexibility. I am sure that there are areas in which it will be hard to accomplish something which can be done in either OS/2 or Windows, but in general this will probably just force programmers to use more consistent interfaces, rather than robbing applications of functionality. When it really comes down to it, a very specific set of controls will get you the vast majority of functionality, and I haven't yet found Java lacking here, except perhaps for the missing multi-line edit field.
Having said that, some things are a little odd in Java's GUI implementation, but you are sure to find these yourself when you get this far. Layout managers are but one oddity. I would have preferred some kind of portable resource format myself, but ain't no such animal in Java that I have found.
MLEs do not have counter-parts in Java, but they can be emulated with a class using several single line edit controls, and such an example is given, yielding a multi-line label, in this case.
The next few chapters illustrate more mundane features, such as reading and writing files, performing UNIX-like filtering, using pipes, and so on. The networking support in Java is surprisingly comprehensive, including things like downloading a URL from the net, although if the downloaded file is HTML, you will have to parse it yourself. There are classes to work with sockets, as well. A client-server example is given.
Advanced graphics and images are explored further in the next chapter. More advanced animation is shown, including tricks like double-buffering and clipping to enhance performance. Also, image filtering is shown in another brief example.
Finally, advanced threads are demonstrated in the final tutorial chapter. Waiting, notifying, synchronizing threads, and multi-threading are all demonstrated, but briefly.
The next part is the Java language reference, which starts with the usual keyword summary, operator precedence list (which is quite similar to C, by the way), modifiers, comment syntax explanation, and so forth. After that, events are broken down into categories, and fonts, colours and cursors are described.
Next is a summary of differences between what features of Java applets and applications can use, what certain other properties are used for, and how they work, and so on.
Applet security is given its own chapter, which is not too surprising, considering the amount of press this one aspect of Java has been getting. To be fair, Java is probably more secure than any other language/API combination, but the fact that the net access and capability is built right into the specification of the language has probably drawn an undue amount of criticism from less enlightened individuals. This is a long overdue step, but of course, it needs to be done right. This chapter, like many of the chapters so far, is quite short, however. Given the reference nature of the book, this is unsurprising, though this may be criticized as inadequate by the above-mentioned individuals.
HTML, HTTP, and Unicode are all given cursory treatment, along with a synopsis of the tools and man pages available in the JDK.
The raison-d'etre for the book is next. This is a full-coverage listing of all classes and methods in the Java specs. Each important class is given a brief explanation, and each method signature is listed. There are no further explanations for the methods, and this is where you really need to have read another book on the Java API first. Many things come natural enough to Window and OS/2 programmers, but some things do not, and need further explanation.
The list of exceptions is short and handy. Again, each class of exceptions is given an explanation, which is usually sufficient here.
The last chapters are a godsend, and should be in every reference. This ups the ante for every reference, and I will now be looking for this features in every referece I review from now on. These chapters offer extensive cross-references for methods, classes, and parameters. You want to know where the Rectangle class is a return parameter? Look in the Returned-By Index. If you need to know where the addPoint() method is defined, look in the Method Defined-In Index. These indices are SO helpful, yet this is the first time I have come across this. Great feature.
If you program in Java, you need this book. If you are thinking about learning Java, get it too. O'Reilly has a really nice habit of pricing their books WAY cheaper than all other publishers, and they consistenly make high-quality books, so everyone should be able to afford this one. They also have the best binding on the market, and their books will acutally lay open without closing.
You WILL need a first book on Java, and I hope to review some soon.
My only complaint about this book is that it attempts to teach some Java, but doesn't go far enough. I would prefer if this book would JUST be a reference, or if it would actually teach more in-depth. The current level is bit too lean to teach stuff properly, and a bit too much to completely disregard. In the defense of the author, it was probably intended as examples of the most frequently used paradigms of Java. As a final note, I give this book a solid A.
Java in a Nutshell, Flanagan
Our Bookstore has reviews and links to Amazon for all books we have reviewed in the past, as well as several books waiting to be reviewed, and others which we recommend.