SCJP6
Ryan Beckett wrote:
This is false because a and b have different hash codes and in order for objects to be equal their hash codes must be equal. Print them.
SCJP6
If the hash codes of two object values are different, the object values are guaranteed to be different. However, if the hash codes of two object values are the same, the object values are not guaranteed to be the same. An additional call to Object.equals() must be made to confirm that the object values are the same. A good hash code algorithm will minimize the chance of two different values having the same hash code.
Ryan Beckett wrote:Sorry fellow, I'm going to have to slap you with a big fat Nooo.
If the hash codes of two object values are different, the object values are guaranteed to be different. However, if the hash codes of two object values are the same, the object values are not guaranteed to be the same. An additional call to Object.equals() must be made to confirm that the object values are the same. A good hash code algorithm will minimize the chance of two different values having the same hash code.
Read This
Like I said, print the hash codes.
Also, hash codes are always relevant when comparing objects, because you have to override Object.equals to make meaningful comparisons which means you have to override Object.hashCode correctly. As as far as Collections go, yes, sets (and don't forget maps) rely on Object.equals as well.
SCJP6
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
All code in my posts, unless a source is explicitly mentioned, is my own.
Ryan Beckett wrote:I really can't into much more detail, but I did find this:
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
java.lang.Object
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).
SCJP6
All code in my posts, unless a source is explicitly mentioned, is my own.
Ruben Soto wrote:Robert, you got it. Look at my previous post for a little more detail, but what you said is exactly true.
SCJP6
Ryan Beckett wrote:Ahhh... very nice. I knew that arrays are objects, but I didn't Object.equals acts that way. So, I guess when you override, then it compares based on meaningful data instead of addresses.
All code in my posts, unless a source is explicitly mentioned, is my own.
Robert O'Leary wrote:
Ruben Soto wrote:Robert, you got it. Look at my previous post for a little more detail, but what you said is exactly true.
Top man Rueben! I was looking at lots of questions today and my head was start to spin. Then it came to me when I took a break!
All code in my posts, unless a source is explicitly mentioned, is my own.
Are we home yet? Wait, did we forget the tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|