aspose file tools*
The moose likes Beginning Java and the fly likes equals and hashcodes method query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals and hashcodes method query" Watch "equals and hashcodes method query" New topic
Author

equals and hashcodes method query

Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432


output:
o=my age is 20and sex is male
hi this is how sting method is being overload6
k=f
k=25669322
f=14978587


as we have learn if two objects are equal ,their hashcodes must be equal as well.

what i want to ask is : here two objects are equal i.e. k and f ,so why their corresponding hashcodes are coming different?




SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

arjun srivastava wrote:as we have learn if two objects are equal ,their hashcodes must be equal as well.

That's right. The programmer is responsible for making that happen, by writing a hashCode() method which corresponds to the equals(Object) method.
what i want to ask is : here two objects are equal i.e. k and f ,so why their corresponding hashcodes are coming different?

Because the programmer didn't make that happen.

(And let me move this post to a suitable forum.)
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

ok
you mean to say hashcode() method must also overirde if we are overirding equals() method
then only we get the hashcodes equal for corresponding equal objects...?

otherwise ...NO
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Yes. If you override one of the two methods, you should override the other, too, and make sure they behave the way they're supposed to.


[Jess in Action][AskingGoodQuestions]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
David Newton wrote:Please UseCodeTags . . .
. . . and indent your code correctly and completely, please.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals and hashcodes method query