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.”
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
…Exactmethods for arithmetic operations and type conversions that always throw an exception on overflow, unlike the built-in language operators.
- When any operand of the
- For-each loop variables are effectively final. The variable
for (T item: items)is considered to be declared anew for each loop iteration, and immutable during that iteration. Therefore,
itemmay 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
- 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
intarray rather than an
- 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.