• Post Reply Bookmark Topic Watch Topic
  • New Topic

equals and hashcodes method query  RSS feed

 
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?



 
Sheriff
Posts: 23439
46
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
 
author and iconoclast
Sheriff
Posts: 24217
38
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.
 
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.
 
Sheriff
Posts: 57834
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

David Newton wrote:Please UseCodeTags . . .

. . . and indent your code correctly and completely, please.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!