equals()
on an array, which is equivalent to ==
JAVA-S0348This method invokes the .equals(Object o)
method on an array. Since arrays do not override the equals
method of Object
, calling equals
on an array is the same as comparing their addresses.
To compare the contents of the arrays, use java.util.Arrays.equals(Object[], Object[])
. To compare the addresses of the arrays, it would be less confusing to explicitly check pointer equality using ==
.
String[] a = new String[10]();
String[] b = new String[10]();
// a and b are populated with the same data.
assert(a.equals(b)); // Fails, address of a not equal to address of b.
// Or
assert(a == b); // Fails for the same reason.
assert(Arrays.equals(a, b)); // Compares contents of a and b.