Effective Java
Rate it:
Open Preview
Started reading January 14, 2019
3%
Flag icon
One advantage of static factory methods is that, unlike constructors, they have names.
Prashant Kumar Rai
1)
3%
Flag icon
A second advantage of static factory methods is that, unlike constructors, they are not required to create a new object each time they’re invoked.
Prashant Kumar Rai
2)
4%
Flag icon
Item 34) provide this guarantee. A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type.
Prashant Kumar Rai
3)
12%
Flag icon
The Liskov substitution principle says that any important property of a type should also hold for all its subtypes so that any method written for the type should work equally well on its subtypes
12%
Flag icon
While there is no satisfactory way to extend an instantiable class and add a value component, there is a fine workaround: Follow the advice of Item 18, “Favor composition over inheritance.”
12%
Flag icon
java.util.Date and adds a nanoseconds field. The equals implementation for Timestamp does violate symmetry and can cause erratic behavior if Timestamp and Date objects are used in the same collection or are otherwise intermixed.
Prashant Kumar Rai
Timestamp class on Equal override violates symmetry
13%
Flag icon
Whether or not a class is immutable, do not write an equals method that depends on unreliable resources. It’s
13%
Flag icon
To avoid this sort of problem, equals methods should perform only deterministic computations on memory-resident objects.
13%
Flag icon
for float fields, use the static Float.compare(float, float) method; and for double fields, use Double.compare(double, double). The special treatment of float and double fields is made necessary by the existence of Float.NaN, -0.0f and the analogous double values;
Prashant Kumar Rai
Float double
14%
Flag icon
An excellent alternative to writing and testing these methods manually is to use Google’s open source AutoValue framework, which automatically generates these methods for you, triggered by a single annotation on the class .
15%
Flag icon
If a class is immutable and the cost of computing the hash code is significant, you might consider caching the hash code in the object rather than recalculating it each time it is requested.
18%
Flag icon
A notable exception to this rule is arrays, which are best copied with the clone method.
19%
Flag icon
Use of the relational operators < and > in compareTo methods is verbose and error-prone and no longer recommended.
20%
Flag icon
classes with public mutable fields are not generally thread-safe.
20%
Flag icon
Note that a nonzero-length array is always mutable, so it is wrong for a class to have a public static final array field, or an accessor that returns such a field.
21%
Flag icon
An immutable class is simply a class whose instances cannot be modified. All of the information contained in each instance is fixed for the lifetime of the object, so no changes can ever be observed.
41%
Flag icon
Enums are, generally speaking, comparable in performance to int constants. A minor performance disadvantage of enums is that there is a space and time cost to load and initialize enum types, but it is unlikely to be noticeable in practice.
Prashant Kumar Rai
Ednum performance
59%
Flag icon
it is possible to return empty collections and arrays without allocating them.
59%
Flag icon
return new ArrayList<>(cheesesInStock);
59%
Flag icon
Collections.emptyList method. If you were returning a set, you’d use Collections.emptySet; if you were returning a map, you’d use Collections.emptyMap. But remember, this is an optimization, and it’s seldom called for.
64%
Flag icon
In summary, the for-each loop provides compelling advantages over the traditional for loop in clarity, flexibility, and bug prevention, with no performance penalty. Use for-each loops in preference to for loops wherever you can.
Prashant Kumar Rai
For each
65%
Flag icon
primitives have only their values, whereas boxed primitives have identities distinct from their values.
65%
Flag icon
primitive types have only fully functional values, whereas each boxed primitive type has one nonfunctional value, which is null,
65%
Flag icon
primitives are more time- and space-efficient than boxed primitives.
65%
Flag icon
Applying the == operator to boxed primitives is almost always wrong.
Prashant Kumar Rai
Boxed Primitive
66%
Flag icon
when you mix primitives and boxed primitives in an operation, the boxed primitive is auto-unboxed. If
Prashant Kumar Rai
Unboxing
66%
Flag icon
To achieve acceptable performance, use a StringBuilder in place of a String to store the statement under construction:
69%
Flag icon
conventions, many of which are contained in The Java Language
73%
Flag icon
Synchronization is required for reliable communication between threads as well as for mutual exclusion.
Prashant Kumar Rai
Synchronisation
74%
Flag icon
The best way to avoid the problems discussed in this item is not to share mutable data. Either share immutable data (Item 17) or don’t share at all. In other words, confine mutable data to a single thread.
Prashant Kumar Rai
Policy to avoid synchronization problem