Java Reference
In-Depth Information
When canonicalization of objects is required, it may be wiser to use a custom canon-
icalizer built on top of
ConcurrentHashMap
; see Joshua Bloch's
Effective Java
™
,
Second
Edition
, Item 69 [Bloch 2008] for details.
Applicability
Confusing reference equality and object equality can lead to unexpected results.
Usingreferenceequalityinplaceofobjectequalityispermittedonlywhenthedefining
classes guarantee the existence of
at most one
object instance for each possible object
value. The use of static factory methods, rather than public constructors, facilitates in-
stance control; this is a key enabling technique. Another technique is to use an
enum
type.
Use reference equality to determine whether two references point to the same object.
Bibliography
[Bloch 2008]
Item 69, “Prefer Concurrency Utilities to wait and notify”
[FindBugs 2008]
ES, “Comparison of String Objects Using
==
or
!=
”
[JLS 2013]
§3.10.5, “String Literals”
§5.6.2, “Binary Numeric Promotion”
[Long 2012]
EXP03-J. Do not use the equality operators when comparing values of boxed
primitives
MET09-J. Classes that define an
equals()
method must also define a
hashCode()
method
70. Understand the differences between bitwise and logical operators
TheconditionalANDandORoperators(
&&
and
||,
respectively)exhibitshort-circuitbe-
havior. That is, the second operand is evaluated only when the result of the conditional
operatorcannotbededucedsolelybyevaluatingthefirstoperand.Consequently,whenthe
result of the conditional operator
can
be deduced solely from the result of the first oper-
and, the second operand will remain unevaluated; its side effects, if any, will never occur.