#REVIEW: Effective Java Book review

Last Updated:  September 15, 2020 | Published: April 29, 2018

In today's #REVIEW post I want to review one of my latest Java books: Effective Java. I was looking for a more advanced book about Java and therefore I found this book from Joshua Bloch. I bought the third edition of this book which was updated on 27th December 2017 for Java 7, 8, and 9. The book titled itself as the “The Definitive Guide to Java Platform Best Practices”. It has about 420 pages and it took me two weeks to read through it. The following topics were covered with best practices advice and example code:

  1. Creating and Destroying Objects
  2. Methods Common to All Objects
  3. Classes and Interfaces
  4. Generics
  5. Enums and Annotations
  6. Lambdas and Streams
  7. Methods
  8. General Programming
  9. Exceptions
  10. Concurrency
  11. Serialization

In this blog post, I will cover my top five Java learnings from this book.

Learning #1: Default interface methods

Since Java 8 we are now able to add default methods to our interfaces. With this feature, you can implement a default version of your method in the interface and the implementing class can override the default implementation as needed. Therefore you are able to extend your interface definition without breaking your existing codebase and you can add further functionality. You declare the default method with the keyword default after the visibility modifier. The following fictional example describes this new feature:

Two possible implementations can look like the following:

Calling both methods results in:

Learning #2: Assign variables and methods to enums

My next learning is about enums in Java. Over the last years, I just used enums to represent a predefined set of values like for currencies: EURO, DOLLAR, YEN, etc. but this was just a small subset of the enum capabilities. You are also able to define internal attributes and methods for your enums. In the book, the author used the  following example where he represented the mathematical operations as enums:

You can use the following snippet to test the enum above:

Learning #3: Underscore in long numbers

The next one is quite small learning but really helpful if you work with numbers with more then four digits. With Java, you can use underscores in your initialization of your numbers (double, float, int, long) for visual separation purposes. Imagine you have a really long number (e.g. amount of money) as a method variable or a static variable:

With the underscore, you can write the same code as:

Learning #4: Working with varargs

While working with public methods of several Java frameworks I came along methods where I could pass 0 to n arguments to a method and up until now I never took time to investigate how this is technically possible. One chapter of the Effective Java book is dedicated to the so-called varargs. With varargs, your method can take 0 to n arguments of the defined type. Internally Java arrays are used to make this possible. You define a vararg argument with three dots like:

Learning #5: Logical sorting objects

My last learning is not new to the Java language but I always implemented the sorting of my domain objects in a different way. For comparing and therefore sorting Java objects you can use the interface Comparable<T>. Implementing this interface you have to provide an implementation of the public int compareTo(T object) method. This method should return zero if the compared objects are equal, a negative value if the compared object is less and a positive value if the compared object is greater then the base object. In the following example, I use the domain object Person which has an attribute age that is used for comparing persons.

For sorting a collection of persons I'll use the method Collections.sort(List<T> list) which will pick up my implemented compareTo method:

Effective Java review summary: Once and for all, I can say the book is definitely worth the 35 € for the Kindle edition. There were a lot of advanced Java topics covered and I got deeper insights into several language features of the latest Java versions.

Find further Java-related book and video course reviews here.

Have fun reading Effective Java,

Phil.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Sign up for Our Mailing List And Get

the Testing Java Applications ($9) Cheat Sheet for Free

Testing Java Applications Cheat Sheet Cover
>