File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes What's wrong with my hashCode() ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What Watch "What New topic
Author

What's wrong with my hashCode() ?

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660



I am calling different instances, however, it returns the same hashCode() for different instances:


returns
172 [main] DEBUG poker.Poker - 217
172 [main] DEBUG poker.Poker - 217
172 [main] DEBUG poker.Poker - 217

Not sure why they are being returned in triplicate either.



returns
172 [main] DEBUG poker.Poker - 217
...(x3)








Jon
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40



It would help if you print out more details -- such as "name" or other parts of the instance, in addition to the hash code. We don't know anything about the instances that you are referring to.

And BTW, it is valid for two unequal instances to have the same hash code -- so there may be nothing wrong.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Henry Wong wrote:

It would help if you print out more details -- such as "name" or other parts of the instance, in addition to the hash code. We don't know anything about the instances that you are referring to.

And BTW, it is valid for two unequal instances to have the same hash code -- so there may be nothing wrong.

Henry


I think I resolved it now:

Output
847 for _name = "Johnny"
847 for _name = "Johnny"
729 for "_name = "Peter"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Why do you start your variable names with underscores?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What's wrong with my hashCode() ?