Updates from Effective Java Third Edition 2nd Edition Personal Notes

The * Effective Java Third Edition * I ordered has finally arrived, so I have summarized the changes from the second edition. I have skipped the personal notes and the parts that are not different from the second edition, so please forgive me for any omissions. Since the second edition refers to the Japanese version, there may be inconsistencies in relation to the translated text. I would be grateful if you could point out any mistakes.

Usage Guide

Quotation from Effective Java Third Edition or its translation

Plaintext-Personal impressions


Item 2

The builder has been added with recursive parameters. You can fluently design subclasses by setting the builder returned by the parent abstract class Pizza toBuilder <T extends Builder <T >>and the type returned by its method ʻaddTopping () to T`. ..

Item 5: Use Dependency injection rather than hard code resources

** Static utility classes and singletons are not suitable for classes whose behavior can be parameterized by resources **

Item 6

The example of creating unnecessary objects has been changed from using Calendar toString.matches ()(the problem is that it creates Pattern each time).

Item 8: Avoid finalizers and cleaners

The cleaners added in Java 9 have been talked about.

** Cleaners aren't as dangerous as finalizers, but they're still unpredictable, slow, and generally unnecessary **

Item 9: Use try-with-resources rather than try-finally

Item 10

Personally, I don't like AutoValue very much, so I use Lombok or write it in Kotlin.

IDEs do not make careless mistakes, and humans do IDEs don't make inadvertent mistakes, but humans do

Item 11

The code example has been modified to use Type.hashCode () (Short.hashCode (), etc.) to generate the hash code. The documentation for hashCode generation has become more pronounced from "don't do it" to "don't do it" (documentation makes it impossible to change in the future).

Item 12

Item 13

Item 14

Item 15

Item 19

Item 20

Item 21: Design the interface for the future

** It is not always possible to write a default method without changing all possible implementations ** ** It is still of utmost importance that great care must be taken when designing an interface **

Old item 21

Deleted, merged with Item 42

Item 24

Item 25: Write only one top-level class in the source file

You can write multiple public classes in a language like Kotlin, but I think it's better to separate them as much as possible for readability.

Item 26: Do not use prototype

"With new code" is gone (not forcibly refactored)

Item 27:

Description of diamond operator

Item 30

With the addition of the diamond operator, the description of the newHashMap utility method for omitting type parameters has disappeared when creating a new collection.

generic singleton pattern

private static UnaryOperator<Object> IDENTITY_FN = (t) -> t;
@SuppressWarnings("unchecked")
public static <T> UnaryOperator<T> identityFunction() {
    return (UnaryOperator<T>) IDENTITY_FN;
}

I don't think I have many opportunities to use it in actual battles.

Item 32: Carefully combine generic types and variadic arguments

Item 34

Convert from String to Enum

private static final Map<String, Operation> stringToEnum = Stream.of(values()).collect(toMap(Object::toString, e -> e));
public static Optional<Operation> fromString(String symbol) {
    return Optional.ofNufllable(stringToEnum.get(symbol));
}

I personally want to throw an exception (discussed in Item 55).

The Payroll Day was subtly refactored.

Item 37

Why did you change Herb to Plant and Type to Lifecycle?

Item 39

JUnit -> JUnit 3 JUnit 4 is annotation-based, so I'm just following the advice in this section.

Item 42: Use lambda instead of anonymous class

Lambda parameter types are not used unless it makes the program cleaner. Lambda lacks name and documentation. Do not use lambdas if the process is not self-explanatory or is larger than a few lines.

Item 43: Use method references instead of lambda

Item 44: Use standard functional interface

The "template method" pattern, in which subclasses override "primitive methods" to customize the behavior of superclasses, is much less attractive. A modern alternative is to provide a static factory or constructor that accepts function objects that do the same thing.

Do not create different overloaded methods with different functional interfaces in the same position as arguments if they can be ambiguous to the client.

Item 45: Use streams with caution

just because you can doesn't mean you should

** Overuse of streams makes the program hard to read and maintain **

My ears hurt

** Using helper methods in code that uses stream pipelines is even more important than using them in code that uses iterators. ** **

The method name (primes) is a plural noun that describes the elements of the stream. This naming convention is highly recommended for all methods that return a stream because it improves the readability of the stream pipeline.

Item 46: Use a function with no side effects in the stream

The forEach instruction should only be used to report the processing results of a stream, not to do any processing.

It's easy to say forEach (e-> list.add (e)).

Item 47: Use collection rather than stream for return value

If you are writing a public API that returns a sequence, you should write it not only for those who want to write a stream pipeline, but also for those who want to write a for-each statement.

** Do not store large sequences in memory just to return them as a collection. ** **

Consider implementing a collection for a special purpose.

Item 48: Carefully parallelize streams

** If the source is Stream.iterate or the intermediate operation limit is used, paralleling the pipeline will not improve performance. ** **

** Parallelizing streams of ʻArrayList, HashMap, HashSet, ConcurrentHashMap instances, arrays, ʻint range, and long range is the biggest performance gain. Let's do it. ** **

It is important to remember that stream parallelization is a performance optimization. As with any optimization, you should measure performance before and after the change to make sure you need to do so.

Item 49

Item 50

** Date is obsolete and should no longer be used in new code **

Item 55: Carefully return optional

** Container-types and options such as maps, streams and arrays must not be optionally wrapped. Declare a method to return ʻOptional ` if it may not return a result, * and *, and the client needs to do something special. ** **

Item 56

Item 59

Old Item 73: Avoid Thread Groups

Delete

Item 75

Item 80: Select executor, task, and stream from thread

The stream has been added as a candidate.

Item 85: Use alternatives rather than Java serialization

The danger of serialization is heavily emphasized.

** The best way to avoid serialization vulnerabilities is to not deserialize anything ** ** There is no reason to use Java serialization on newly written systems **

The next best thing is to ** never deserialize untrusted data **

In conclusion, serialization is dangerous and should not be used.

Impressions

Bonus-Mr. Brock's PC

Old new
AMD Opteron 170 Core i7-4770K
2GB RAM DDR3-1866 16GB RAM
Windows XP Windows 7 Professional SP1
JDK 1.6 Java HotSpot Azul Zulu 9.0.0.15

Recommended Posts

Updates from Effective Java Third Edition 2nd Edition Personal Notes
Effective Java 2nd Edition Related Matters Survey Summary
Personal comment criteria [Java edition]
From Ineffective Java to Effective Java
What has changed between Effective Java 2nd Edition and 3rd Edition
Effective Java 3rd Edition Chapter 8 Methods
Effective Java 3rd Edition Chapter 9 Program General
Effective Java 3rd Edition Chapter 6 enums and annotations
Effective Java 3rd Edition Chapter 4 Classes and Interfaces
Effective Java 3rd Edition Chapter 7 Lambda and Streams