Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

As a general rule (subject to the exceptions below), use the Object.equals() method to check whether two objects are abstractly equal to each other, and . Reserve use of the equality operators == and != only to test for testing whether two references specifically refer to the same object (this is reference equality). See also guideline MET13-J. Ensure that hashCode() is overridden when equals() is overridden.

When operating on numeric boxed types (e.g.,Further confusion arises because the numerical comparison operators <, <=, >, and >= can be used with the numeric boxed types Byte, Character, Short, Integer, Long, Float, and Double. In this case, auto-unboxing results in ), the numeric relational operators (e.g., <, <=, >, 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 being compared, with the expected results (see JLS Section 5.6.2, "Binary Numeric Promotion"). When But when both arguments of a an equality operator (e.g., == or != operator ) are of a numeric boxed type, the equality operators are defined to be reference equality operators; the operation is thus a reference comparison rather than the anticipated numeric comparison, which may produce unexpected results (see EXP03-J. Avoid the equal and not equal operators when comparing values of boxed primitives).

...

The reference equality operator == evaluates to true when and only when the values it compares reference the same underlying object. This noncompliant example declares two distinct String objects that contain the same value. The references, however, compare as unequal because they reference distinct objects.

Code Block
bgColor#FFcccc
public class ExampleComparison {
  public static void main(String[] args) {
    String one = new String("one");
    String two = new String("one");
    boolean result;
    if (one == null){ // test below is redundant in this case, but required for full generality
    if (one == null) { 
      result = two == null;
    }
    else {
      result = one == two;
    }
    System.out.println(result); // prints false
  }
}

Compliant Solution (Object.equals())

To be compliant, use This compliant solution uses the Object.equals() method when comparing string values.

Code Block
bgColor#ccccff
public class GoodComparison {
  public static void main(String[] args) {
    String one = new String("one");
    String two = new String("one");
    boolean result;
    if (one == null) {
      result = two == null;
    }
 else   else{
      result = one.equals(two);
    }
    System.out.println(result);
  }
}

...

Reference equality produces the desired result when comparing string literals (e.g. for example, String one = "one"; and String two = "two";) or when the intern method has been used on both strings.

When a task requires both keeping only one copy of each string in memory as well as performing quick repeated string comparisons, the following Compliant Solution this compliant solution may be used.

Code Block
bgColor#ccccff
public class GoodComparison {
  public static void main(String[] args) {
    String one = new String("one");
    String two = new String("one");
    boolean result;
    if (one != null){
      one = one.intern();
    }
    if (two != null){
      two = two.intern();
    }
    result = one == two;

   System.out.println(result);
  }
}

Use this approach with care; performance and clarity may be better served by use of code that applies the Object.equals() approach and lacks a dependence on reference equality.

General Rule

Assume that object comparisons require use of Object.equals() unless the defining classes specifically document their compliance with exception EXPO1-EX1 below. Reserve use of the == and != operators for testing reference equality. See also guideline MET13-J. Ensure that hashCode() is overridden when equals() is overridden.

Exceptions

EXP01-EX1: Use of reference equality in place of object equality is permitted when, and only when , the defining classes guarantee the existence of at most one object instance for each possible object value. This generally requires that instances of such classes are immutable. The use of static factory methods rather than public constructors facilitates instance control; this is a key enabling technique.

...