Java


JAVA.COMPARE.EQ : Should Use equals() Instead of == (Java)

要旨

Two objects are compared with == but equals() seems more appropriate instead.

This checker identifies incorrect or inefficient comparisons through equals() or ==. In some cases, these comparisons are wrong: for instance, strings should be compared for equality through equals() rather than through ==. Classes can be safely compared through == instead. In other cases, these comparisons can be replaced by more efficient code.

プロパティ

クラス名 Should Use equals() Instead of == (Java)
日本語クラス名 Should Use equals() Instead of == (Java)
クラス分類 信頼性 (reliability)
ニーモニック JAVA.COMPARE.EQ
カテゴリー
CWE CWE:595 Comparison of Object References Instead of Object Contents
CERT-Java CERT-Java:EXP02-J Do not use the Object.equals() method to compare two arrays
  CERT-Java:EXP03-J Do not use the equality operators when comparing values of boxed primitives
対応言語 Java で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Should Use equals() Instead of == (Java)"

// Main.java
public class Main {
  public static boolean verbose;
  private final static String[] arr = new String[] { "verbose" };

  public static void main(String[] args) {
    if (args.length > 0)
      assert !args[0].equals("");                // Comparison to Empty String (Java)    
                                                 // warning issued here. 

    if (args.length > 0 && args[0] == "verbose") // Should Use equals() Instead of == (Java)     
                                                 // warning issued here. 
      verbose = true;

    if (args.equals(arr))                        // equals on Array (Java)    
                                                 // warning issued here. 
      verbose = true;

    if (args.length > 0) {
      Main m = new Main();
      System.out.println(m.test(args[0]));
      System.out.println(m.weAreRedefined());
    }
  }

  private boolean test(Object o) {
    return o == this;                            /* == Always Fails (Java)     
                                                  * warning issued here (objects are of incompatible type:  
                                                  * a String and an instance of Main. 
                                                  */ 
  }

  public boolean weAreRedefined() {
    return !getClass().equals(Main.class);       // Should Use == Instead of equals() (Java)
                                                 // warning issued here. 
  }
}

Each of these warnings corresponds to incorrect or inefficient comparisons. The programmer should rewrite the code as follows.

// Main.java, after modification
public class Main {
  public static boolean verbose;
  private final static String[] arr = new String[] { "verbose" };

  public static void main(String[] args) {
    if (args.length > 0)
      assert !args[0].isEmpty();

    if (args.length > 0 && args[0].equals("verbose"))
      verbose = true;

    if (java.util.Arrays.equals(args, arr))
      verbose = true;

    if (args.length > 0) {
      Main m = new Main();
      //System.out.println(m.test(args[0]));
      System.out.println(m.weAreRedefined());
    }
  }

  public boolean weAreRedefined() {
    return getClass() != Main.class;
  }
}

解決法

Check if the equality is actually wrong or can be replaced by more optimized code.

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。