...
As a general rule, use the Object.equals()
method to check whether two objects are abstractly equal to each other. Reserve use of the equality operators ==
and !=
for testing whether two references specifically refer to the same object (this is reference equality). See also guideline MET13-J. Classes that define an equals() method must also define a hashCode() method.
When operating on This also applies to numeric boxed types (for example,Byte
, Character
, Short
, Integer
, Long
, Float
, and Double
), although the numeric relational operators (such as <
, <=
, >
, and >=
) produce results that match those provided for arguments of the equivalent primitive numeric types. Specifically, the JLS requires auto-unboxing in this case, which results in comparison of the numeric values contained in the boxed objects. ( See JLS Section 5.6.2, "Binary Numeric Promotion".) But when both arguments of an equality operator (for example, ==
or !=
) are of a numeric boxed type, the operation is a reference comparison rather than the anticipated numeric comparison, which can produce unexpected results. (See guideline EXP03-J. Avoid the equal and not equal operators when comparing values of boxed primitives for more information.)
Noncompliant Code Example
...
Code Block | ||
---|---|---|
| ||
public class ExampleComparison { public static void main(String[] args) { String onestr1 = new String("one"); String twostr2 = new String("one"); boolean result; // test below is redundant in this case, but required for full generality if (onestr1 == null) { result = twostr2 == null; } else { result = onestr1 == twostr2; } System.out.println(result); // prints false } } |
...
Code Block | ||
---|---|---|
| ||
public class GoodComparison { public static void main(String[] args) { String onestr1 = new String("one"); String twostr2 = new String("one"); boolean result; if (onestr1 == null) { result = two(str2 == null); } else { result = onestr1.equals(twostr2); } System.out.println(result); } } |
Compliant Solution (String.intern())
Reference equality produces the desired result when comparing string literals (for example, String one = "one";
and String two = "two";
) or when the intern
method has been used on both strings.behaves like abstract object equality when comparing two strings that are each the result of the String.intern()
method. When a task requires keeping only one copy of each string in memory, as well as performing quick repeated string comparisons, this compliant solution may be used.
Code Block | ||
---|---|---|
| ||
public class GoodComparison { public static void main(String[] args) { String onestr1 = new String("one"); String twostr2 = new String("one"); boolean result; if (onestr1 != null) { onestr1 = onestr1.intern(); } if (twostr2 != null) { twostr2 = twostr2.intern(); } result = onestr1 == twostr2; System.out.println(result); } } |
Use this approach with care; performance and clarity could be better served by use of code that applies the Object.equals()
approach and lacks a any dependence on reference equality.
...
EXP01-EX2: Use reference equality to determine whether two references point to the same object instance.
Risk Assessment
Using reference equality to compare objects may lead to unexpected results.
...