Calling ==
is a reference comparison, ie using memory location to decide equality. If both do not contain the same reference, it will return false.
Calling equals
on an object instance will use the implementation of equals
in that class; if it doesn't have one that overrides the default, it falls back to the generic one in Object
. This is the Object.equals
description from the docs:
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
In short if you don't implement your own equals
method you'll get the default behavior which will behave just like ==
. For details on writing your own equals
method, check this very comprehensive community wiki.