Raw Notes: Revisiting Effective Java in 2019
-
Credentials
-
Lambdas
-
Streams
-
Optionals
-
Default methods in interfaces
-
try with resources
-
factory methods
-
Minimize mutability
-
Comparable(14)
-
Live coding
-
Minimize mutability. Value object class. Why? Easier to reason about. Reduces the chance that the system gets into an inconsistent state.
-
Make as many of things
finalas possible -
No ctor, use factory method:
of. Easier for refactoring. Allows you to change the cardinality, maybe re-use objects with an object pool. Flyweight class.BigDecimal.valueOf()implementation is an example. -
COMMENT: what about making the zero arg ctor private?
-
He used Google Guava, but noted that they have violated backward compatibility in the past.
-
Advised to implement
equalsandhashCode. Use theObjectsclass in the JDK. I thas anequalsmethod. Handles all thatnullchecking. But beware of the performance penalty of autoboxing. For hashcode, there isObjects.hash. This is even worse for performance. If you use the generator forequalsandhashcodemake sure to keep the impls up to date as the class evolved.- for hashcode, because it is immutable, you can do it in the
ctor. If you use an object as a key to a hashmap, it must be
immutable. If you can’t do that, you can make a
toHashKey()method.
- for hashcode, because it is immutable, you can do it in the
ctor. If you use an object as a key to a hashmap, it must be
immutable. If you can’t do that, you can make a
-
toString(). It’s for developers. GuavaMoreObjects.toStringHelper(). Not for “business strings”. For busness strings, useformatTowithFormatterandFormattable. -
Comparable.Comparatorinterface.
-
-
Functional Interfaces.
@FunctionalInterface. Constrains your code to conform to functional interfaces. Favor Strategy over Template Method.- You should not need to create a new functional interface. There
are 41 already in the JDK. It’s likely it will be in there
already. Learn these six well:
UnaryOperator, BinaryOperator, Predicate, Function, Supplier, Consumer.
- You should not need to create a new functional interface. There
are 41 already in the JDK. It’s likely it will be in there
already. Learn these six well:
-
Method References. Five different types
-
static, ie
Integer::parseInt() -
bounded and unbounded method reference.
-
-
Enums
- Can use lambdas or function references in enum initializer.
-