and also if two hashcode are equals then it doesnot means that object will be equal
it means they are in same bucket
Martin Vanyavchich
Ranch Hand
Joined: Sep 16, 2008
Posts: 241
posted
0
For singleton instance '==' is what you should be using, since in case of objects it compares their location in memory.
SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Chrix Wu
Ranch Hand
Joined: Nov 15, 2009
Posts: 121
posted
0
Thank you all, folks.
By the way, Shanky Sohar, when you said "use equals to check whether two objects are meaningfully equal?"
what does that mean?
In other words, what is the inner implementation under equals method?
Martin Vanyavchich
Ranch Hand
Joined: Sep 16, 2008
Posts: 241
posted
0
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.
Dieter Quickfend
Ranch Hand
Joined: Aug 06, 2010
Posts: 280
posted
0
== 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.
Oracle Certified Professional Java Programmer
Chrix Wu
Ranch Hand
Joined: Nov 15, 2009
Posts: 121
posted
0
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.
Ok, i see, we should override equals() in order to give it more meaningful object comparision.
BUT what if i dont override the equals() method in my own class, what is the equals() actually doing?
What is it doing in order to decide to return true or false?
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
4
posted
0
. . . and the equals() method in the class you are using should have documentation comments to describe how it works.
Martin Vanyavchich
Ranch Hand
Joined: Sep 16, 2008
Posts: 241
posted
0
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).