In Java, Arrays.deepEquals(a, b) returns true if two arrays are equal at every level: same length, and each pair of elements is equal, where elements that are themselves arrays are compared by their contents too.
A 2D array is an array of row objects. Arrays.equals compares those rows with their own equals, which only checks identity. deepEquals goes into each row (and each row of each row) and compares the actual values.
A static method of java.util.Arrays (Java 5+): call it on the class, as in Arrays.deepEquals(...); there is no Arrays object to create.
Syntax
Pass two object arrays; nested arrays are compared deeply.
Java
int[][] a = { {1, 2}, {3} };
int[][] b = { {1, 2}, {3} };
Arrays.equals(a, b); // false: rows compared by reference
Arrays.deepEquals(a, b); // true: rows compared by contents
Call
What it does
Arrays.deepEquals(grid1, grid2)
Compare two 2D (or deeper) arrays.
Arrays.deepEquals(objs1, objs2)
Mixed contents: plain objects and arrays.
Arrays.deepEquals(null, null)
true.
Arrays.deepEquals(a, null)
false when only one is null.
There is only one overload, taking two Object[]. Primitive arrays nested inside, such as the rows of an int[][], are compared with the matching Arrays.equals overload.
same shape with null row: true
null row vs empty row: false
both arrays null: true
How It Works
Rows of different lengths. The first rows have one and two elements; deepEquals compares each pair of rows on its own, so jagged shapes are fine.
null matches null. Both third rows are null, which counts as equal, so the first result is true.
null is not empty. An empty row {} is a real array of length 0, so it does not equal a null row.
No cast needed. There is only one deepEquals overload, so deepEquals(null, null) compiles as is and returns true.
Frequently Asked Questions
Use Arrays.deepEquals(a, b). Arrays.equals compares the rows as objects and usually returns false for separate but identical 2D arrays.
equals compares elements with their own equals method, which for nested arrays means identity. deepEquals compares nested arrays by their contents, at any depth.
Not directly, because int[] is not an Object[]. Use Arrays.equals for flat primitive arrays; deepEquals handles int[] rows inside an int[][].
Yes. Two null arrays are equal, a null row equals only a null row, and nothing throws an exception.
Yes. Rows of different lengths are compared one pair at a time; arrays with different row lengths are simply not equal.