Those who have read the Third Edition of Effective Java are likely aware of the source code associated with that book available on GitHub. The jbloch/effective-java-3e-source-code project has 1700+ stars and has been forked nearly 800 times as of this writing. The version of Java featured in the Third Edition of Effective Java is largely JDK 8 with some coverage of JDK 9 (see my earlier post for details on what is covered in this third edition).
Much has been added to the JDK since the publication of the Third Edition of Effective Java and many new releases have arrived with the faster 6-month cadence. Given this, I was particularly interested to see in an amber-spec-experts mailing list post that Rémi Forax has forked jbloch/effective-java-3e-source-code into the GitHub project forax/effective-java-3e-source-code that has "taken the source of Effective Java (3rd Ed) and change them to use var, switch expression, records and the instanceof with the type test pattern."
There are several things that I like about the idea of refreshing examples from Effective Java (Third Edition) to use newer features:
- Developers can see how to apply effective Java practices using recently released features.
- Developers can view the differences between the JDK 8/9 versions and the newer versions to see how new constructs replace older constructs and thus gain a better understanding of the newer constructs.
- It is useful to see some of the changes when deciding whether a particular change to use a newer construct really helps with code readability in a given situation.
The main page for the forked forax/effective-java-3e-source-code (README.md) states, "The source code have been updated to use new constructs available since Java 9, the version used by the 3rd edition." That page then provides bullets on the types of new constructs applied to the source code with links to each new construct's associated JDK Enhancement Proposal (JEP).
As of this writing, Commit 275eef87e4661f7f1edc41f4730cecf7a1096a97 is the main commit of interest. It covers changes to 113 files. I'll call out a few specific changes here to illustrate the types of changes applied (some of which are to apply preferred constructs that were available even before JDK 9):
NutritionFacts.java
(Chapter 2, Item 2) changed from a class to a record.PickTwo.java
(Chapter 5, Item 32) change demonstrates switch expressions.CaseInsenstiveString.java
(Chapter 3, Item 10) change demonstrates application of instanceof with type test pattern.WordList.java
(Chapter 3, Item 14) changed to apply var for variable declarations taking advantage of local type inference. There are many of these changes, but this is one I liked. I'm not convinced that some of the changes in other places (such as changingint
tovar
) are helpful. The change forInstrumentedSet
(Chapter 4 Item 18) is even more compelling.- Change to
RecursiveTypeBound.java
(Chapter 5, Item 31) demonstrates usingvar
infor
loop. Copy.copy
method (Chapter 2, Item 9) changed to accept instances of Path instead of instances of String and changed to use Files.newInputStream and Files.newOutputStream in place ofnew FileInputStream
andnew FileOutputStream
respectively.
Conclusion
The ability to view changes to the original source code associated with the Third Edition of Effective Java to accommodate new language constructs is highly useful in terms of learning about the new constructs and how they relate to or replace old constructs and in deciding if the differences are desirable in different situations.