Java Arrays.deepEquals() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Arrays

Definition

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
CallWhat 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.

Library declarations
Java
// java.util.Arrays (since Java 5)
public static boolean deepEquals(Object[] a1, Object[] a2)

Parameters

ParameterDescription
a1, a2 (Object[])The arrays to compare. Either may be null, and their elements may be arrays or null.

Return Value

Return ValueDescription
booleantrue if the two arrays are deeply equal (or both null); otherwise false.

Compare with Similar Methods

Nested-array helpers and their flat versions:

MethodHow it differs
Arrays.equals(a, b)One level only; right for int[] and String[].
Arrays.deepHashCode(a)Hash code that agrees with deepEquals.
Arrays.deepToString(a)Print nested arrays to see where they differ.
Arrays.mismatch(a, b)Index of the first difference in flat arrays (Java 9).

Java Examples

Example 1 — Compare Two Game Boards

Two boards with the same layout, checked three different ways.

Java
import java.util.Arrays;

public class ArraysDeepEqualsDemo {
    public static void main(String[] args) {
        int[][] board1 = { {1, 0}, {0, 1} };
        int[][] board2 = { {1, 0}, {0, 1} };

        System.out.println("same object: " + (board1 == board2));
        System.out.println("equals:      " + Arrays.equals(board1, board2));
        System.out.println("deepEquals:  " + Arrays.deepEquals(board1, board2));
    }
}

How It Works

  1. Four row objects. Each board literal creates its own outer array and its own two rows.
  2. Shallow comparison fails. Arrays.equals compares board1[0] with board2[0] using Objects.equals, which for arrays means identity, so it returns false.
  3. Deep comparison succeeds. deepEquals sees that both rows are int[] and compares them with Arrays.equals(int[], int[]).
  4. Use the deep version for grids. Any time elements are arrays, deepEquals is the method that matches what "equal boards" means.

Example 2 — Arrays Inside Object Arrays

Compare records that hold a string, an int[], and a 2D String array.

Java
import java.util.Arrays;

public class ArraysDeepEqualsNested {
    public static void main(String[] args) {
        Object[] a = {"id", new int[] {1, 2}, new String[][] { {"x"}, {"y"} }};
        Object[] b = {"id", new int[] {1, 2}, new String[][] { {"x"}, {"y"} }};
        Object[] c = {"id", new int[] {1, 2}, new String[][] { {"x"}, {"z"} }};

        System.out.println("a vs b: " + Arrays.deepEquals(a, b));
        System.out.println("a vs c: " + Arrays.deepEquals(a, c));
        System.out.println(Arrays.deepToString(c));
    }
}

How It Works

  1. Different kinds of elements. Each record holds a String, an int[], and a String[][], and deepEquals picks the right comparison for each.
  2. Three levels. For the last element it goes into the 2D array, then into each row, and compares "x" with "x" and "y" with "y".
  3. One letter decides. Record c has "z" in the deepest row, so a and c are not equal.
  4. See the difference. deepToString(c) prints the whole structure, which helps when tracking down why two records differ.

Example 3 — null Rows, Empty Rows, and null Arrays

Jagged arrays whose last row is null in one case and empty in another.

Java
import java.util.Arrays;

public class ArraysDeepEqualsNulls {
    public static void main(String[] args) {
        int[][] jagged1 = { {1}, {2, 3}, null };
        int[][] jagged2 = { {1}, {2, 3}, null };
        int[][] jagged3 = { {1}, {2, 3}, {} };

        System.out.println("same shape with null row: " + Arrays.deepEquals(jagged1, jagged2));
        System.out.println("null row vs empty row:    " + Arrays.deepEquals(jagged1, jagged3));
        System.out.println("both arrays null:         " + Arrays.deepEquals(null, null));
    }
}

How It Works

  1. 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.
  2. null matches null. Both third rows are null, which counts as equal, so the first result is true.
  3. null is not empty. An empty row {} is a real array of length 0, so it does not equal a null row.
  4. 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.

Next: Arrays.deepHashCode()

Compute a content-based hash code for nested arrays.

deepHashCode() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful