Search code examples
equalityjava-7deepequals

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


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()?


Solution

  • If at least one of the arguments of deepEquals method is not an array, then Objects.deepEquals and Objects.equals are same.