In Java, Arrays.equals(a, b) returns true when both arrays have the same length and every pair of elements at the same index is equal. Order matters: {1, 2} and {2, 1} are not equal.
You need it because a == b and a.equals(b) only check whether a and b are the same array object. Two separate arrays with identical contents are "different" to both of those, but equal to Arrays.equals.
A static method of java.util.Arrays (Java 1.2+): call it on the class, as in Arrays.equals(...); there is no Arrays object to create. Java 9 added overloads that compare a range of each array and that compare object elements with a Comparator.
Syntax
Pass two arrays of the same type.
Java
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
boolean same = Arrays.equals(a, b); // true
boolean ref = (a == b); // false: two objects
Arrays.equals(a, new int[] {3, 2, 1}); // false: order matters
Call
What it does
Arrays.equals(a, b)
Same length and equal elements, index by index.
Arrays.equals(null, null)
true (with typed null variables).
Arrays.equals(a, 0, 2, b, 1, 3)
Java 9+: compares a[0..2) with b[1..3).
Arrays.equals(x, y, comparator)
Java 9+: object elements are equal when the comparator returns 0.
The two-array form exists for every primitive type and for Object[]. Object elements are compared with Objects.equals(e1, e2), so two null elements count as equal.
Library declarations
Java
// java.util.Arrays (since Java 1.2)
public static boolean equals(int[] a, int[] a2)
public static boolean equals(Object[] a, Object[] a2)
// also long[], short[], char[], byte[], boolean[], float[], double[]
// Java 9+: range form equals(a, aFrom, aTo, b, bFrom, bTo), and
public static <T> boolean equals(T[] a, T[] a2, Comparator<? super T> cmp)
Parameters
Parameter
Description
a, a2
The two arrays to compare. Either may be null.
range indexes (Java 9+)
From (inclusive) and to (exclusive) indexes for each array.
cmp (Java 9+)
A comparator that decides element equality for object arrays.
Return Value
Return Value
Description
boolean
true if both are null, or both have the same length and equal elements; otherwise false.
NaN is never == itself. By the floating-point rules, NaN == NaN is false.
Arrays.equals says yes. It compares doubles the way Double.equals does, where NaN equals NaN, so x and y are equal.
Two zeros.0.0 == -0.0 is true with the == operator.
Different bit patterns.Double.equals tells 0.0 and -0.0 apart, so Arrays.equals(x, z) is false.
Frequently Asked Questions
Arrays inherit equals() from Object, which only checks whether both references point to the same array. Use Arrays.equals(array1, array2) to compare contents.
Not as you might expect. It compares the rows as objects, so two separate but identical 2D arrays are usually not equal. Use Arrays.deepEquals.
No. Elements must match index by index. Sort copies of both arrays first if order should not matter.
true if both are null, false if only one is null. It does not throw.
Yes, from Java 9: Arrays.equals(a, aFrom, aTo, b, bFrom, bTo) compares two ranges.