wood burning stoves 2.0*
The moose likes Java in General and the fly likes How overrided HashCode() is being called in this method?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How overrided HashCode() is being called in this method??" Watch "How overrided HashCode() is being called in this method??" New topic
Author

How overrided HashCode() is being called in this method??

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1056



http://www.lifesbizzare.blogspot.com || OCJP:81%
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

Your equals method is wrong. When you pass null or an instance of an incompatible class, the method should return false. In your implementation it will throw a NullPointerException for null and a ClassCastException for incompatible objects.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1056

Rob Spoor wrote:Your equals method is wrong. When you pass null or an instance of an incompatible class, the method should return false. In your implementation it will throw a NullPointerException for null and a ClassCastException for incompatible objects.


Yes Correct but how an Object is assigned an integer value using hashCode() method in this case?
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1512
    
    5

Well, I'm not a hashCode and equals expert (and I haven't seen the code of Object class which invokes these methods), but, as per my understanding, this is what happens:

1) hasCode method determines which 'bucket' the object belongs to. There can be multiple objects in same bucket.
2) equals method searches further within the bucket to extract the exact object. Here, I'm using bucket as a terminology assuming that you are extracting a particular object from a collection of your own objects (e.g. HashHash objects - by the way, why that name is given to class?)
3) So, there is a chance that hashCode of two objects are same, but those objects are not equal (i..e equals method returns false).
4) Hence, it doesn't make any sense (to me) to call hashCode from equals method. As I've said, I don't know how the real code works, but I will not call equals method unless hashCode methods on both objects - the one I'm trying to find and the one from collection - return same values.

And yes, as Rob Spoor has pointed out, the first thing you should do in your equals method is:
1) If input is null, return false
2) If input is not IS-A current object, return false
3) Proceed with your logic

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1056

Anayonkar Shivalkar wrote:Well, I'm not a hashCode and equals expert (and I haven't seen the code of Object class which invokes these methods), but, as per my understanding, this is what happens:

1) hasCode method determines which 'bucket' the object belongs to. There can be multiple objects in same bucket.
2) equals method searches further within the bucket to extract the exact object. Here, I'm using bucket as a terminology assuming that you are extracting a particular object from a collection of your own objects (e.g. HashHash objects - by the way, why that name is given to class?)
3) So, there is a chance that hashCode of two objects are same, but those objects are not equal (i..e equals method returns false).
4) Hence, it doesn't make any sense (to me) to call hashCode from equals method. As I've said, I don't know how the real code works, but I will not call equals method unless hashCode methods on both objects - the one I'm trying to find and the one from collection - return same values.

And yes, as Rob Spoor has pointed out, the first thing you should do in your equals method is:
1) If input is null, return false
2) If input is not IS-A current object, return false
3) Proceed with your logic

I hope this helps.


Hi ,

I am reading the SCJP6 book and same terminology of the buckets have been used. My only query is how come Object is assigned a hashcode int value by overriding hashCode()..
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

I'm not sure that question makes sense. Can you explain more what you mean? hashCode() isn't called by any of the code you've written, but may be called by other classes as they want. HashSet is just one example of a class that will use it.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1512
    
    5

Vishal Hegde wrote:I am reading the SCJP6 book and same terminology of the buckets have been used. My only query is how come Object is assigned a hashcode int value by overriding hashCode()..

Well, that depends on what your hasCode method returns.

e.g. You have an Employee class with different members like id, name, department etc. and your hashCode method returns something based on department, then you can say that there are different buckets for each department. If your hashCode based on department follows the proper contracts, then for two same departments, hashCode value would be same - i.e. those objects would be in same bucket.

Now, two employees to be equal, lets assume that their departments and names must be same. So, you'll put that code accordingly in the equals method.

When you'll try to search an employee based on your current object, first, it'll understand which bucket to search in - based on hashCode. So, for sales employee, the code (i.e. code of corresponding collection) would check out sales bucket.

Further, within those objects, it'll pick up an employee which has same name as your current employee (in short, it'll pick up something whose equals method will return true).

So, you see, we assign hashCode to an object based on our own logic. hashCode method is gonna be called on that object anyway. By overriding it, we can put our own logic. If we don't override it, then hashCode of Object class would be called.

I hope this helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How overrided HashCode() is being called in this method??