Friday, July 3, 2009

2009 JavaOne: Still Effective Java

As evidenced by several previous blog posts (including What Makes a Great Technical Book?) I am a big fan of the book Effective Java. It therefore is not surprising that one of the first 2009 JavaOne presentations that I looked at when the slides were made available was Joshua Bloch's Effective Java: Still Effective, After All These Years (TS-5217).

This presentation begins by comparing the Second Edition of Effective Java to the First Edition in terms of number of items covered in each edition. The major new topics (including a completely overhauled chapter on concurrency) are also highlighted. I like that the Second Edition of the book contains a table at the end of the book that provides a mapping of items in the First Edition to items in the Second Edition.

Focus of Presentation

The Effective Java: Still Effective, After All These Years presentation focuses on the new items in the Second Edition: Generics, Enum Types, Variable Arguments, Concurrency, and Serialization (the "new" focus here is the Serialization Proxy Pattern). It's no surprise that the content of these slides is similar to select content from the book.


Bloch uses several slides of this presentation to cover use of wildcards with generics and covers the acronym he introduced in the second edition: PECS -> Producer Extends, Consumer Super. This mnemonic is a variation of the Get and Put Principle that Naftalin and Wadler discuss in Java Generics and Collections (one of the deepest Java books I have read).

Whether it is PECS or "Get and Put," the principle is the same: use the extends keyword with generics wildcards when retrieving data (get) from a source data structure (producer extends) or use the super keyword with generics wildcards when inserting data (put) into a data structure (consumer super). As he does in the book, Bloch points out in the slides that these generics wildcards should only be used for method arguments and not for method return values. Also, it is worth noting that it is nonsensical to have a single method argument use both extends and super.


Bloch covers some advantages of enums in this presentation. I already love the Java enum and don't need any convincing in this case. Compared to generics, enums are very simple to understand and apply. In fact, my biggest problem with enums is that I'm often tempted to stretch them past what they are intended for. I didn't see it covered in this presentation, but one of the things Bloch points out in the Second Edition of Effective Java that has really grown on me is the idea of an enum-based singleton.

Variable Arguments

Throughout Effective Java, Bloch repeatedly mentions that many of the covered items are "effective" because they move the reporting of problems to as early as possible in the process. Specifically, many of the best practices in his book lead to issues being discovered at compile time rather than at runtime. In the second edition of the book and in this presentation, Bloch points out that using variable arguments recklessly can lead to runtime problems. Specifically, he recommends always explicitly specifying the first argument of a variable arguments list as a single argument followed by varargs syntax for the remaining potential parameters of that same type when at least one is required. This prevents the runtime error if a method is called without any arguments. It will instead be discovered at compile time. In other words, the compiler can enforce the requirement of at least one argument being provided.

Concurrency / Threading

J2SE 5 saw dramatic changes to Java's concurrency support and the changes to the second edition of Effective Java reflect that. Several slides in this presentation focus on concurrency. This is the portion of the presentation where I think the liner notes from Bloch's verbal presentation would be especially helpful.


Bloch discusses the Serialization Proxy Pattern as a better way of dealing with some common limitations associated with serialization in Java. He uses code to demonstrate the pattern in practice and also notes that this approach will not always be appropriate.

Some of My Favorite Items in Second Edition

Two of the items that the Second Edition of Effective Java newly covers that helped me justify purchasing a new edition do not seem to receive focus in this presentation, but they are covered in the excerpt available from the book itself: Chapter 2: Creating and Destroying Objects. I use the Item 2 recommendation (Builder implementation rather than telescoping constructor) and the recommendation at the very end of Item 3 (enum implementation of Singleton) relatively frequently. I also use static factories in many cases, but that was the first item in the First Edition as well.

Benefiting from this Presentation

Different Java developers are likely to benefit from this presentation in different ways. For those of us who own (or have borrowed) the Second Edition and have read it, most of this material is not new. However, it is still useful to see it again and can sometimes be helpful to see it in different form (slides rather than book pages).

For those who own the Second Edition, but have not read it, this presentation gives a taste of what's available there and might provide motivation to invest time in reading. For those who own the First Edition and are thinking about purchasing the Second Edition, this presentation provides a nice taste of the types of things newly covered in the Second Edition, though a presentation of this length could not adequately cover all the concepts and ideas introduced with the Second Edition of Effective Java.

Other Coverage

There are other resources out there that review this presentation or that are related to this presentation. They are too numerous to list them all here, but I do list a few:

JavaOne 2009: Effective Java

Effective Java, Second Edition, Chapter 2

Effective Java, Second Edition, Chapter 5

Book Excerpt and Interview: Effective Java, Second Edition

Book Review: Effective Java Second Edition

Effective Java -- Still Effective?

Photograph of Presentation

No comments: