How to check if two arrays are equal in Java?

To check if two arrays are equal in Java, call the Arrays.equals method.

Arrays.equals compares elements of arrays at same positions using the equals method (or the == operator for primitive types):

int[] array1 = new int[] { 1, 2, 3 };
int[] array2 = new int[] { 1, 2 };

Arrays.equals(array1, array1); // true
Arrays.equals(array1, array2); // false