Book Review: Effective Java 2nd Edition

Effective Java is  book in the Effective series, started by Scott Meyers with the excellent book Effective C++. The general format of the series is to present topics of high importance for intermediate to advanced users. The goal is not to teach the mechanics of the language, but the fine topics that make the difference between medi0cre users of language and advanced programmers.

Effective Java was written by Josh Bloch, one of the members of the group that defined the Java libraries. Therefore, Effective Java is a book that provides lessons based on the deep experience of the author with the design of core Java libraries. In particular, it is nice to hear from him what he thinks is wrong with some of the Java libraries, so that you don’t need to repeat the same mistakes the Java creators made.

Focus on Immutable Objects

One of the best topics of the book, and one that I advocate myself through the years, is that of trying to make objects immutable as much as possible. The idea is that objects should be in states that are well defined, and therefore the smaller the number of states, the easier it is to understand what the code is doing. Examples of using the pattern of immutable objects in Java is the String class: after construction, String objects are immutable, and any operation will create a new String that is returned as a result.

Traditional programming techniques use variables as a free-for-all, where state changes as much as needed. But once you learn functional programming, you will start to realize that values don’t need to change for your program to do something useful, and the less they change, the easier it is to figure out what is going on. In fact, in functional programming you just need to apply functions and check the results of the application — nothing else.

Of course, imperative languages such as Java still need variables on loops, but even this is not so important nowadays that we have foreach loops in all major languages other than C++.

Improvements in the 2nd edition

Many topics changed from the 1st edition of Effective Java, but the most important change was in the coverage of generics. The implementation of generics in Java is interesting and surprising, because of their use of the type erasure. That is, parametrized types in Java exist only as viewed by the compiler, and the information is erased from the resulting class files. This makes the resulting code compatible with previous JVMs, so the same code works everywhere. However, it has dangerous downsides, which require the full understanding from programmers.
Effective Java provides a deep introduction to the subject of Java generics, and after reading it you will not probably like generics a little less — but will also be more prepared to use it. The pros and cons of using Array objects instead of native arrays are explained. Also, the important concepts of extends and super are in generic types is summarized using the PECS acronym (produces-extends, consumer-super).    

[ad name=”amazon-effective-java”] Effective Java 2nd edition delivers what it promises. All the basic points in using Java, from creating objects, to managing resources and using generic types are discussed. Maybe some of these topics could be extended in newer editions, or even in another book. But it provides all the necessary material to transform a beginner into a well educated Java programmer.

Similar Posts:

About the Author

Carlos Oliveira holds a PhD in Systems Engineering and Optimization from University of Florida. He works as a software engineer, with more than 10 years of experience in developing high performance, commercial and scientific applications in C++, Java, and Objective-C. His most Recent Book is Practical C++ Financial Programming.

Post a Comment