• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

equals hashcode doubt sjcp 1.4

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a doubt with equals and hascode methods. I test this code.



If two objects are equal, their hashCodes must return the same integer. In this example the hascode isn't the same, because it isn't implemented and it returns the hashCode of class Object, that always returns diferents hashcode for diferent objects.

I don't understand why this code works and why the hashMap is able to find the objects if the last is true. If the contract is only valid for hash, why does this code works?

And if I don't want to use the TestEqualsHashcode class objects to use in keys of hash, why I must override hashCode if I override equals? The code compile and works


Tanks in advance. Kind regards
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been studying all day and my head hurts so I haven't put much effort on your code, but I did run and look at it quickly. I'm not quite sure what's unpredictable to you, but you create objects using 'new' operator:
new TestEqualsHashcode(1) and even though they have the same constructor argument, they are in fact unique objects given that each one is using the new to create, well, a new object in memory. So each one will have a unique hashcode representation which uniquely identifies each key for the map. If you look at the default implementation of hashcode method in object you'll see that it basically creates a hashcode based on converting the memory address to an int hashcode. So when you print those out it shows the hashcodes like you'd expect. And you were able to get the correct elements back because you had unique hashcode created by object's hashcode method. However, your equals method tests instanceof and num. All is good so you get true for all of them. But what if you create a new element by copying the object, changing the 'num' and then adding it to the map? Check out this code from your example. Note the bold part and then trace the code and run and the result is much different because you can't depend on unique hashcode from Object's implementation of hashcode. I should have also done one where the num stays the same but I'll let you play with that LOL. I think you'll javascript: x()get better replies if you keep your code snippets a bit shorter by the way (sorry if I can't be more helpful but I'm too tired to think hard LOL):


[ December 13, 2007: Message edited by: nico dotti ]
 
nico dotti
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's another run at it:

 
mathew jonson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nico, I'm grateful for your efforts and I understand you have said. But I have no clear the concept yet. There are a lot of question like that: "if you override equals, you MUST override hashcode", and the only one reason for this
is to have a good hash. The MUST word, may be changed by this: "If you override equals, and if you want to use the objects as a key in a hash collection, then override properly hashcode".
The only diference that I have found without override hashcode is that internally, I have a non optimized hash. Am I wrong?
reply
    Bookmark Topic Watch Topic
  • New Topic