Core Java 8 for the Impatient

Core Java for the Impatient — Cay S. Horstmann, Addison-Wesley 2015 (for Java SE 8), ISBN 978-0-321-99632-9

This 480-page book is a compromise between Horstmann’s brief introduction Java SE 8 for the Really Impatient (215 pages) and the traditional monumental Core Java tomes (2092 pages for Java SE 7). Rather than covering virtually the entire standard library and JDK toolset, For the Impatient focuses on essential functionality that should be relevant to any project, whether end-user application or web server component. Specifically, the following Core Java topics are missing entirely: graphics & user interfaces (both AWT/Swing and JavaFX), deployment & security, JavaBeans, XML, networking, databases, distributed objects, and native methods.

That accounts for about 1,100 pages, so the remaining coverage shrank by half to reach the new 480-page size – and that’s ignoring the new Java SE 8 features. While For the Impatient is neither a beginner’s tutorial nor an exhaustive reference, the covered subjects are for the most part sensibly selected and treated with surprising thoroughness. Horstmann crams an incredible amount of essential knowledge and useful tips into the available space. The discussion of streams nicely elucidates this powerful but unconventional new API, and Horstmann delivers the best concise explanation of wildcard variance in Java’s obscure generics I’ve seen yet. Lambda expressions are treated as a special case of interfaces, which is unusual but makes perfect sense: both are contractual specifications for behavior, and Java even defines lambdas in terms of “functional interfaces.”

I was a bit baffled by the extensive coverage of annotations and the new Nashorn JavaScript engine. Typically only specialist tool creators write their own annotations, and only web developers integrating existing JS code would care about Nashorn. The contents of these chapters are as good as the rest, though. There are also drastically fewer errors than in the rather sloppily edited Core Java. I found only a handful of typos in the sample code, none in the text. Until a new Core Java revision is available, every Java SE 8 programmer should read this book.

Today I Learned…

As always, the above review was added to my Developer Books archive. But reading the book made me aware of some important language & library features I had previously ignored, so I’ve also updated Java for C# Programmers with the following items:

  • The utility class java.lang.Math received various …Exact methods for arithmetic operations and type conversions that always throw an exception on overflow, unlike the built-in language operators.
  • When any operand of the + operator is a string, the sum computed so far in left-to-right direction is automatically converted to a string, and so are all other remaining individual operands (JLS §15.18.1). Whoever thought such JavaScript-like behavior was a good idea?
  • For-each loop variables are effectively final. The variable item in for (T item: items) is considered to be declared anew for each loop iteration, and immutable during that iteration. Therefore, item may be accessed by any lambda expression declared within the loop body. See Loop Closures in Java & C# for more details.
  • Method references to superclass members are expressed as super::<i>method</i>.
  • Constructor references for typed arrays, such as int[]::new, can be passed to generic array creation methods that expect an IntFunction<T[]> argument, such as Stream.toArray. This allows the method to create and return a correctly typed int array rather than an Object array.
  • Due to type erasure, static fields in generic classes are shared among all type instantiations, and generically typed static fields are forbidden. (They would collapse into one Object-typed field for all generic type instantiations.) This is the polar opposite of C# which allocates new storage space for all static fields, even non-generic ones, for each type instantiation of a generic class.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.