Objects.deepEquals Compares Nested Arrays

Compare arbitrary Object references holding primitive arrays, nested Object[] graphs, and nulls safely using Objects.deepEquals instead of ==.

JavaData Integrity

Code

snippet
import java.util.Objects;

public class GenericArrayEquality {

    public static void main(String[] args) {
        Object left = new int[] { 10, 20, 30 };
        Object right = new int[] { 10, 20, 30 };

        // Objects.equals delegates to array.equals(), which checks reference identity only
        System.out.println("Objects.equals(int[]):     " + Objects.equals(left, right));
        // Objects.deepEquals inspects primitive and nested Object[] arrays recursively
        System.out.println("Objects.deepEquals(int[]): " + Objects.deepEquals(left, right));

        Object[] treeA = { "cfg", new int[] { 1, 2 }, new String[] { "a", "b" } };
        Object[] treeB = { "cfg", new int[] { 1, 2 }, new String[] { "a", "b" } };
        System.out.println("shallow equals(Object[]):  " + Objects.equals(treeA, treeB));
        System.out.println("deepEquals(Object[]):      " + Objects.deepEquals(treeA, treeB));

        // Null inputs never throw NullPointerException
        System.out.println("null == null: " + Objects.deepEquals(null, null)
                + ", array == null: " + Objects.deepEquals(left, null));
    }
}

Annotated execution — Objects.deepEquals Compares Nested Arrays

The moving parts are the class GenericArrayEquality and the function main.

What to watch for

  • Core compiler and runtime alone suffice — zero package resolution occurs.

Using it

The entry-point signature is public static void main(String[] args)

The file’s steps show up in the output as Objects.equals(int[]):, Objects.deepEquals(int[]):, shallow equals(Object[]):.

Key statements

Object[] treeA = { "cfg", new int[] { 1, 2 }, new String[] { "a", "b" } };
Object[] treeB = { "cfg", new int[] { 1, 2 }, new String[] { "a", "b" } };
Object right = new int[] { 10, 20, 30 };

Interactive Tools for This Topic

FAQ

What does Objects.deepEquals Compares Nested Arrays print when run?
Running the file directly is enough — expect labeled output lines such as `Objects.equals(int[]):`, `Objects.deepEquals(int[]):`, `shallow equals(Object[]):`.