Effective Java
Effective Java
com/blog/effective-java/
MISHADOFF THOUGHTS
Search ARCHIVE CATEGORIES
ABOUT RSS
Effective Java
Recently, I’ve re-read awesome java book Effective Java by Joshua Bloch.
The book contains 78 independent items, discussing various aspects of
programming in java. Something like mini-design patterns with emphasis
on their pros and cons.
1 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
enum Singleton {
INSTANCE
}
2 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
3 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
4 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
5 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
6 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
7 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
▪ Eliminate every unchecked warning that you can, that means your
code is typesafe
▪ Use @SuppressWarnings("unchecked") only if you can prove the code
is typesafe
▪ Always use the @SuppressWarnings annotation on the smallest scope
possible
▪ Every time you use an @SuppressWarnings annotation, add a comment
saying why it’s safe to do so
▪ Every unchecked warning represents the potential for a
ClassCastException at runtime. Do not ignore them blindly
8 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
9 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
10 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
11 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
a declaration in a supertype
▪ @Override helps to catch tricky bugs (overloaded equals, hashcode)
▪ Use the @Override annotation on every method declaration that you
believe to override a superclass declaration
12 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
needed
▪ You must program defensively, with the assumption that clients of
your class will do their best to destroy its invariants
▪ If you return mutable reference from class, then class is also mutable
▪ To make immutable class, make a defensive copy of each mutable
parameter
▪ Defensive copies are made before checking the validity of the
parameters and the validity check is performed on the copies rather
than on the originals (protect against changes from another thread,
TOCTOU time-of-check/time-of-use attack)
▪ Do not use the clone method to make a defensive copy of a parameter
whose type is subclassable by untrusted parties
▪ Defensive copying of parameters is not just for immutable classes,
think twice before returning a reference
▪ Arrays are always mutable
▪ Defensive copying can have a performance penalty associated with it
and isn’t always justified
▪ If the cost of the defensive copy would be prohibitive and the class
trusts its clients not to modify the components inappropriately, then
the defensive copy may be replaced by documentation outlining the
client’s responsibility not to modify the affected components
13 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
14 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
15 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
16 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
17 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
▪ Grammatical
18 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
19 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
try {
// Use lower-level abstraction to do our bidding
} catch(LowerLevelException e) {
throw new HigherLevelException();
}
20 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
21 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
▪ To avoid liveness and safety failures, never give control to the client
within a synchronized method or block
▪ Alien methods may cause data corruption, deadlocks
▪ Java locks are reentrant
▪ CopyOnWriteArrayList is a variant of ArrayList in which all write
operations are implemented by making a fresh copy of the entire
underlying array
▪ Do as little work as possible inside synchronized regions
▪ When in doubt, do not synchronize your class, but document that it is
not thread-safe
22 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
▪ To prevent DOS attack, you can use a private lock object instead of
using synchronized methods
23 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
scheduler
▪ Any program that relies on the thread scheduler for correctness or
performance is likely to be nonportable.
▪ The best way to write a robust, responsive, portable program is to
ensure that the average number of runnable threads is not
significantly greater than the number of processors
▪ Do not rely on Thread.yield because it has no testable semantics
▪ Use Thread.sleep(1) instead of Thread.yield() for concurrency
testing
▪ Thread priorities are among the least portable features of the Java
platform
24 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
25 of 26 12/21/23, 11:06
Effective Java https://mishadoff.com/blog/effective-java/
time
▪ Use enum types to enforce instance control invariants wherever
possible
Post
26 of 26 12/21/23, 11:06