All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.java.rules.squid.S1294.html Maven / Gradle / Ivy

Since arrays do not override Object.equals(), calling equals on two arrays is the same as comparing their addresses. This means that array1.equals(array2) is equivalent to array1==array2.

However, some developers might expect Array.equals(Object obj) to do more than a simple memory address comparison, comparing for instance the size and content of the two arrays. To prevent such a misunderstanding, the '==' operator or Arrays.equals(array1, array2) must always be used in place of the Array.equals(Object obj) method.

Noncompliant Code Example

String[] array1 = ...
String[] array2 = ...
...
if(array1.equals(array2)){
...
}

Compliant

String[] array1 = ...
String[] array2 = ...
...
if(array1 == array2){
...
}
//or
if(Arrays.equals(array1, array2)) {
  ...
}

See

Tool Reference Description
Findbugs EC_INCOMPATIBLE_ARRAY_COMPARE Correctness - equals(...) used to compare incompatible arrays
Findbugs EC_BAD_ARRAY_COMPARE Correctness - Invocation of equals() on an array, which is equivalent to ==




© 2015 - 2025 Weber Informatics LLC | Privacy Policy