Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objects as Map keys without Hashcode and equals.

 
Ved Antani
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Output is:
first
second


How does this "get" method behave ? As both m1 and M2 have same values and I have not overridden hashcode(), will Object class's equals() method be called ?
I think the behaviour is like this:
1. There is no hashcode method so there is no way for the JVM to see if objects m1 and m2 contain different values
2. There is no equals method overridden so Object class's equals() is invoked and as both objects are different the code above works fine without m2 replacing m1's value.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48976
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your interpretation is correct
 
Henry Wong
author
Marshal
Pie
Posts: 21125
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ved Antani wrote:
Output is:
first
second


How does this "get" method behave ? As both m1 and M2 have same values and I have not overridden hashcode(), will Object class's equals() method be called ?
I think the behaviour is like this:
1. There is no hashcode method so there is no way for the JVM to see if objects m1 and m2 contain different values
2. There is no equals method overridden so Object class's equals() is invoked and as both objects are different the code above works fine without m2 replacing m1's value.


1. There is no hashCode() method defined so it will be inherited from the Object class. This method will try to create a unique value per instance -- this value is also called the identity hash code.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic