aspose file tools*
The moose likes Beginning Java and the fly likes When does hashCode gets Called !! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When does hashCode gets Called !!" Watch "When does hashCode gets Called !!" New topic
Author

When does hashCode gets Called !!

Puja Agarwal
Greenhorn

Joined: May 29, 2008
Posts: 11
Hi all,

When does the hashCode method in a class gets called.

If we are putting the objects is HashCode a few methods are add , equals , contains , containsAll , toString .

And what if i put the object in HashMap when will the hashCode method of that class be called ..


Thanks in advance
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

The hashCode() method is used by collection classes such as HashSet and HashMap. It will be called by those collection classes when you add or lookup objects. If you want to know when it's called on your object, then add for example a System.out.println(...) call in your hashCode() method:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
That hashCode method is called on the "K" objects in a Map whenever it is used as a Key for "put" or "get" or it is sought with "containsKey."
I don't think it is used for "add" or "equals" which are not actually Map methods.

Go into your Java installation folder, find a file called src.zip, unzip it, go into the java folder, then util, then find a class which implements Map, eg HashMap. You can read the code and see how the hash code is used.
Puja Agarwal
Greenhorn

Joined: May 29, 2008
Posts: 11
Thanks to both of you, just one last doubt ; i found it weird the hasCode method gets called even if i print the HashCode object .

I am pasting the code along with output






Please Explain ...

Regards
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Whenever you print any object , the objects toString() method gets called , which , in turn prints some meaningful information explaining it . But if that class dosent override toString() method, the object simply print its hash code value !

Correct me if i am wrong in explaining !

Thanks,


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

When you print a HelloEx method with System.out.println(), the toString() method is called on the HelloEx object to convert it to a string that can be printed.

Since your HelloEx class does not have its own toString() method, the toString() method of class Object is called. And the toString() of class Object returns a string of the form:

ClassName@HashCodeHex

(the name of the class, an @, and the hash code of the object in hexadecimal format). So, Object.toString() is calling your hashCode() method at that time.
Puja Agarwal
Greenhorn

Joined: May 29, 2008
Posts: 11
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When does hashCode gets Called !!