File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes hashCode( ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "hashCode( )" Watch "hashCode( )" New topic

hashCode( )

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I have the following output:

Calculated hash=4 for JOHN DOE
students[hash]: JOHN DOE : Java
students hashCode value = 21990827

Could someone explain why I got the hashCode value = 21990827 and NOT 4? (4 is what I was expecting?

Do I need to have a toString( ) method? Similar to when I wish to have something more meaningful when printing out the contents of an array?

Thanking you in advance!
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

You need to override hashCode() in Student. If you wanted the hashCode() for a Student object to be the same as the hashCode() for the Student's name, then you could write

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: hashCode( )
It's not a secret anymore!