Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals and hashcodes method query

 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?



 
Paul Clapham
Sheriff
Pie
Posts: 20964
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Please UseCodeTags . . .
. . . and indent your code correctly and completely, please.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic