On Java 7's equals() and deepEquals()

Viewed 15718

Method description says:

Returns true if the arguments are deeply equal to each other and false otherwise... Equality is determined by using the equals method of the first argument.

Which (to me) suggests that Objects are deeply equal if every object they maintain references to are also equal using the equals() method. And every objects they have a reference to are also equal. And ..

So .. equality is determined by using the equals method of the first argument.

How is this different from .equals()? Assuming that we describe equals appropriately where, objects is equal to another object is every field of the object is equal to it as well.

Can you please provide an example illustrating the difference between Objects.deepEquals() and Objects.equals()?

5 Answers

Example:

import java.util.Arrays;
import java.util.Objects;

public class Main {
    public static void main(String[] args) {
        Integer[] x = { 1, 2 };
        Integer[] y = { 1, 2 };
        System.out.println(Objects.equals(x, y));       // false
        System.out.println(Objects.deepEquals(x, y));   // true
        System.out.println(Arrays.equals(x, y));        // true
        System.out.println(Arrays.deepEquals(x, y));    // true
        System.out.println();

        int[][] a = { { 1, 2 }, { 3, 4 } };
        int[][] b = { { 1, 2 }, { 3, 4 } };
        System.out.println(Objects.equals(a, b));       // false
        System.out.println(Objects.deepEquals(a, b));   // true
        System.out.println(Arrays.equals(a, b));        // false
        System.out.println(Arrays.deepEquals(a, b));    // true
    }
}

Documentation and decompiled code:

Objects#equals(Object a, Object b): Returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned. Otherwise, equality is determined by using the equals method of the first argument.

public static boolean equals(Object a, Object b) {
    return (a == b) || (a != null && a.equals(b));
}

Objects#deepEquals(Object a, Object b): Returns true if the arguments are deeply equal to each other and false otherwise. Two null values are deeply equal. If both arguments are arrays, the algorithm in Arrays.deepEquals is used to determine equality. Otherwise, equality is determined by using the equals method of the first argument.

public static boolean deepEquals(Object a, Object b) {
    if (a == b)
        return true;
    else if (a == null || b == null)
        return false;
    else
        return Arrays.deepEquals0(a, b);
}

Arrays#equals(Object[] a, Object[] a2): Returns true if the two specified arrays of Objects are equal to one another. The two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal.

public static boolean equals(Object[] a, Object[] a2) {
    if (a==a2)
        return true;
    if (a==null || a2==null)
        return false;

    int length = a.length;
    if (a2.length != length)
        return false;

    for (int i=0; i<length; i++) {
        if (!Objects.equals(a[i], a2[i]))
            return false;
    }

    return true;
}

Arrays#deepEquals(Object[] a1, Object[] a2): Returns true if the two specified arrays are deeply equal to one another. Unlike the equals(Object[],Object[]) method, this method is appropriate for use with nested arrays of arbitrary depth.

public static boolean deepEquals(Object[] a1, Object[] a2) {
    if (a1 == a2)
        return true;
    if (a1 == null || a2==null)
        return false;
    int length = a1.length;
    if (a2.length != length)
        return false;

    for (int i = 0; i < length; i++) {
        Object e1 = a1[i];
        Object e2 = a2[i];

        if (e1 == e2)
            continue;
        if (e1 == null)
            return false;

        // Figure out whether the two elements are equal
        boolean eq = deepEquals0(e1, e2);

        if (!eq)
            return false;
    }
    return true;
}

static boolean deepEquals0(Object e1, Object e2) {
    assert e1 != null;
    boolean eq;
    if (e1 instanceof Object[] && e2 instanceof Object[])
        eq = deepEquals ((Object[]) e1, (Object[]) e2);
    else if (e1 instanceof byte[] && e2 instanceof byte[])
        eq = equals((byte[]) e1, (byte[]) e2);
    else if (e1 instanceof short[] && e2 instanceof short[])
        eq = equals((short[]) e1, (short[]) e2);
    else if (e1 instanceof int[] && e2 instanceof int[])
        eq = equals((int[]) e1, (int[]) e2);
    else if (e1 instanceof long[] && e2 instanceof long[])
        eq = equals((long[]) e1, (long[]) e2);
    else if (e1 instanceof char[] && e2 instanceof char[])
        eq = equals((char[]) e1, (char[]) e2);
    else if (e1 instanceof float[] && e2 instanceof float[])
        eq = equals((float[]) e1, (float[]) e2);
    else if (e1 instanceof double[] && e2 instanceof double[])
        eq = equals((double[]) e1, (double[]) e2);
    else if (e1 instanceof boolean[] && e2 instanceof boolean[])
        eq = equals((boolean[]) e1, (boolean[]) e2);
    else
        eq = e1.equals(e2);
    return eq;
}
Related