2005-02-19

"Core Java 2: Volume II-Advanced Features"

I haven't bought a Java book in some time. Why bother, when you have the source, the JavaDoc, and Sun's online tutorial?

Recently, amazon.com offered Cay Horstmann's "Core Java 2: Volume II-Advanced Features" with about $20 off, just as I was thinking it was about time I made an effort to have a look at the new stuff in 1.5; help myself avoid the trap of using familiar old stuff when there are better modern alternatives. I read a sample chapter on the web, and it look okay.

The book is fat. The kind of 1,000-page tome that you might associate with world-recognized morons like Herbert Schildt. But Anguish et al's Cocoa Programming is fat too, and that's a good enough book that I feel bad for mentioning it in the same breath as Schildt.

The first chapter is "Multithreading", which was one of my main interests in terms of 1.5 features: the new concurrency package is something I'm really going to be able to use. I was pleased to find that we get past the "what are threads?" quickly, and move on to various common tasks, what your choices are, and how to discriminate between them.

The author is good at explaining how things used to be in addition to how they are now (and why you'd want to go with the modern way). This is going to be increasingly useful as people come to Java who don't remember the old days; they're going to see plenty of crufty code and not know when they're looking at bogus magic. A strange exception is that when ThreadGroup is discussed, there's no mention of the fact that there's no longer any reason to use it.

(As an aside, I like TimeUnit. That's a great solution to the problem of, well, time units. I explained it to someone I work with, and they were unimpressed, saying it's "obvious". Which is exactly why it's so good. And surprising that neither of us has seen it anywhere else.)

I didn't like the way that, when mentioning fairness, the author kept saying "significant performance penalty". What does that mean? Without a specific application in mind, no-one can say whether a performance penalty is significant or not. If he meant "fairness shouldn't be your default choice because it isn't free", he should have said so.

There's some mention of Swing and threading issues, covering both invokeLater/invokeAndWait and SwingWorker. There's no mention of Foxtrot, which is a pity because from what little I've seen it looks like the nicest way to write that kind of thing — write it as if it were synchronous. I'd like someone to go there for me and find out what works and what doesn't, and come back and tell me if I should use it too.

Next up was "Collections", the chapter I'd read on the web. I liked this chapter. Even if you know the collections classes well, this is a good chapter. I was mostly interested in the new stuff, but I learned a few things about the old stuff too.

The chapter on "Networking" had good but brief coverage of socket timeouts and interruptible socket I/O. I was disappointed in the example network server. ThreadedEchoServer would have been a good place to show thread pools in action. And also a good place to demonstrate java.nio (annoyingly covered in volume I).

Tie-ins between chapters (and good examples) are a common problem with books. It's hard to come up with example code that does something interesting, illustrates a point or two, and is small enough to be understood in a book.

Which brings me to my problem with this book: the unconventional interpretation of "Advanced". Why is NIO not "advanced"? What's "advanced" at all in the very weak "Advanced Swing" chapter? Its coverage of text especially is anything but advanced. The chapter after that on "Advanced AWT" is better, with good coverage of the clipboard, but it tries to do too much and falls way short of its title. "Advanced AWT" needs a book, not a chapter. These chapters come nowhere near the standard set by the book's first two chapters. Sun's Java tutorial has more advanced coverage of AWT and Swing.

I guess that in the context of this book, "advanced" doesn't mean anything more than "not in volume I".

(Kim Topley's book "Core Swing: Advanced Programming", which seems like a permutation of this book's title, is the best book I've seen on Swing's text components. I like Sun's tutorial and the source well enough for the rest. I haven't come across a book on Graphics2D that I really liked, and I tend to read the source when I need to know more about AWT. There's some weird and interesting stuff in there.)

The chapter on "JavaBeans Components" almost got skipped. I mean, who cares? They're so 1996. It turns out to contain an interesting section about XML serialization. (The chapter doesn't explain why java.beans uses a hack instead of using the transient modifier like normal serialization. It probably didn't hurt me to think about it and realize that the answer is that the hack has to work on methods rather than fields, but whenever I do something that's obviously a hack, I feel obliged to explain the motivation and why I don't believe one can do a better/simpler job.)

The chapters on "Security", "Database Programming", "Distributed Objects", and "Internationalization" didn't interest me much. "Native Methods" would have sucked for me because the major example is Windows-only. Sun's JNI tutorial is pretty good anyway, and I have the JNI book too.

The "XML" chapter was good. It covered the variety of choices more clearly than usual, and was refreshingly free of XML hype. All the choice look like they're still way more work than they should be, but there's some interesting stuff coming in 1.5 (XPath and XSLT). This is the one chapter that I've actually used in anger; I replaced some code that read and wrote plain text files with code to read/write XML.

The book ends with "Annotations". It's strange that the emphasis should be on user-defined annotations rather than the standard annotations. Especially when annotations are such unexplored territory. I need to know more about them myself before I can really judge this chapter, but there was nothing there that made me want to rush out and try something.

Overall, the plus points are good overviews of concurrency, collections, and XML. Negatives include most of the other chapters. Can anything that's treated in Sun's Java Tutorial to a similar or greater depth be reasonably described as "advanced"?

I'd have put the supposedly "advanced" AWT and Swing chapters in the first volume, and saved material on subjects like NIO and reflection for the second volume. If you were new to Java and read both books, I guess you'd be happy enough with them as they are, but you'd be misguided if you ended up thinking you had advanced AWT or Swing knowledge.

Despite how negative this might have sounded, I'd still say this is a book worth having around for the three chapters I praised.