Dieter Quickfend wrote:== will work here, the equals method won't, and I'll tell you why:
== checks for identity, meaning it will only return true if the objects compared have the same address in memory
equals() checks for meaningful equivalency, meaning it will return true whenever the equals() method returns true. Those conditions depend on which implementation of equals() the developer of the first class you're comparing has used. There are rules for creating an appropriate equals class, but you don't always have a guarantee that the implemented equals() method is appropriate. Even if it were appropriate, it would probably only check if the contents are equal, which, if your method "getInstance()" say, returns a new instance for every call, would also result in true, which is definitely not what you want.
which means if you want to make sure it is returning the same item, and not an equivalent other instance, you must use ==.
I hope that is sufficient explanation.
Martin Vanyavchich wrote:It depends on programmers opinion, or the innerworks of the program. For example if you mada a person class with all kinds of attributes, from name, height, age, skin tone, eye color, IQ etc. your equals method might take all of them into consideration or just name and surname.
You can read the whole thing here
JavaDoc wrote: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).