Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of Objects.deepEquals method

The question is about static methods Objects.deepEquals class (since Java 7):

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);
}

As it said in javadoc of this method:

Returns true if the arguments are deeply equal to each other and false otherwise.

What I do not understand: where is the depth of comparison? As we can see inside its implementation it just does references comparison, and inside Arrays.deepEquals0(a, b) for simple Object and Object arguments it invokes just: eq = e1.equals(e2);. So in what kind of sense two objects are deeply equal?

like image 481
Andremoniy Avatar asked Dec 30 '15 09:12

Andremoniy


2 Answers

The comparison would be deep, if you passed Array objects.

Non-array objects will not be evaluated deeper than what you get with equals .

So the depth isn't relevant in your case :

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.

Quoted from :

Object.deepEquals

like image 89
Arnaud Avatar answered Nov 19 '22 23:11

Arnaud


You can refer: Your's Deeply - Why Arrays.deepEquals When We Have Arrays.equals

Arrays.deepEquals looks really deep

From the source, we could understand that Arrays.deepEquals

  1. Loops through the input arrays, gets each pair
  2. Analyses the type of each pair
  3. Delegates the equal deciding logic to one of the overloaded Arrays.equals if they are one of the primitive arrays
  4. Delegates recursively to Arrays.deepEquals if it is an Object array
  5. Calls the respective object’s equals, for any other object
like image 21
Rahul Tripathi Avatar answered Nov 19 '22 21:11

Rahul Tripathi