Java defines the equality operators ==
and !=
for testing reference equality but uses the Object.equals()
method defined in Object
and its subclasses for testing abstract object equality. Naive Naïve programmers often confuse the intent of the ==
operation with that of the Object.equals()
method. This confusion is frequently evident in the context of processing String
objects.
...
Numeric boxed types (for example, Byte
, Character
, Short
, Integer
, Long
, Float
, and Double
) should also be compared using Object.equals()
and not rather than the ==
operator. While reference equality may appear to work for Integer
values between the range -128 −128 and 127, it may fail if either of the operands in the comparison are outside that range. Numeric relational operators other than equality (such as <
, <=
, >
, and >=
) can be safely used to compare boxed primitive types (see EXP03-J. Do not use the equality operators when comparing values of boxed primitives for more information).
...
The Java Language Specification (JLS) [JLS 2013] provides very limited guarantees about the implementation of String.intern()
. For example,
- The cost of
String.intern()
grows as the number of intern strings grows. Performance should be no worse than O(n log n), but the Java Language Specification JLS lacks a specific performance guarantee. - In early Java Virtual Machine (JVM) implementations, interned strings became immortal: they were exempt from garbage collection. This can be problematic when large numbers of strings are interned. More recent implementations can garbage-collect the storage occupied by interned strings that are no longer referenced. However, the Java Language Specification lacks the JLS lacks any specification of this behavior.
- In JVM implementations prior to Java 1.7, interned strings are allocated in the
permgen
storage region, which is typically much smaller than the remainder rest of the heap. Consequently, interning large numbers of strings can lead to an out-of-memory condition. In many Java 1.7 implementations, interned strings are allocated on the heap, relieving this restriction. Once again, the details of allocation are unspecified by the Java Language SpecificationJLS; consequently, implementations may vary.
String interning is also a favorable technique for may also be used in programs that accept repetitively occurring strings. Its use boosts the performance of comparisons and minimizes memory consumption.
When canonicalization of objects is required, it may be wiser to use a custom canonicalizer built on top of ConcurrentHashMap
; see Joshua Bloch's Effective Java, second edition, Item 69 [Bloch 2008], for details.
Applicability
Using Confusing reference equality and object equality to compare objects can lead to unexpected results.
Using reference equality in place of object equality is permitted only when the defining classes guarantee the existence of at most one object instance for each possible object value. This generally requires the class to be a singleton. The use of static factory methods, rather than public constructors, facilitates instance control; this is a key enabling technique. Another technique is to use an enum
type.
...
[Bloch 2008] | Item 69, "Prefer Concurrency Utilities to wait and notify " |
ES, "Comparison of String Objects Using | |
§3.10.5, "String Literals" |
...