Java

Java

Made by DeepSource

Invocation of equals on an array, which is equivalent to == JAVA-E0348

Bug risk
Major
Autofix

This 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 ==.

Bad Practice

String[] a = new String[10]();
String[] b = new String[10]();

// a and b are populated with the same data.

assertTrue(a.equals(b)); // Fails, address of a not equal to address of b.

// Or

assertTrue(a == b); // Fails for the same reason.

Recommended

Use java.util.Arrays.equals instead.

assertTrue(Arrays.equals(a, b)); // Compares contents of a and b.

References