Reference equality used to compare arrays


The problem

Generally when comparing arrays for equality, the programmer intends to check that the contents of the arrays are equal rather than that they are actually the same object. But many commonly used equals methods compare arrays for reference equality rather than content equality. These include the instance .equals() method, Guava’s, JDK’s java.util.Objects#equals(), and Android’s

If reference equality is needed, == should be used instead for clarity. Otherwise, use java.util.Arrays#equals() to compare the contents of the arrays.


Suppress false positives by adding the suppression annotation @SuppressWarnings("ArrayEquals") to the enclosing element.