0

I was thinking hashcodes are only implemented in HashMap, Hashtable. In my understanding hashCode value will be same for both on object level also. Hence

        String str="Niks";
        String str1=new String("Niks");
        System.out.println(str.hashCode());
        System.out.println(str1.hashCode());

The same has codes are returned because on object level the hashcode will be implemented as below. Correct me if i am wrong.

 result = prime * result + ((str == null) ? 0 : str.hashCode()); 
 result2 = prime * result + ((str1 == null) ? 0 : str1.hashCode()); 

Output:

75268767 75268767

niks
  • 1,063
  • 2
  • 9
  • 18
  • I think that you are misunderstanding the difference between equality and identity, and that hash code relates to equality only. – Andy Turner Jan 29 '16 at 14:06
  • @wero: I think the proposed duplicate question does not apply. the proposed duplicate describes how duplicate hashcode values are allowed, while I think the OP is not getting the point of value-based equality. – Nathan Hughes Jan 29 '16 at 14:43

2 Answers2

2

Generally if I'm using strings as keys in a hashmap, I don't want to have to worry about whether the string I'm using as a key for a lookup is exactly the same reference as what I may have inserted previously, that would complicate things immensely. I want to be able to create a string as a key and know that, if the map has a string with the same value as what I'm using, that the map will find it. So comparing references is not what I want, I want to compare by value.

For objects like Strings or numbers (java.lang.Integer, java.math.BigInteger, java.math.BigDecimal), typically comparing references is not useful; all people are interested in is the value. These are called value objects, and equality and hashCode are based strictly on the object's value, not on comparing references.

From a definition posted by Martin Fowler:

In P of EAA I described Value Object as a small object such as a Money or date range object. Their key property is that they follow value semantics rather than reference semantics.

You can usually tell them because their notion of equality isn't based on identity, instead two value objects are equal if all their fields are equal. Although all fields are equal, you don't need to compare all fields if a subset is unique - for example currency codes for currency objects are enough to test equality.

A general heuristic is that value objects should be entirely immutable. If you want to change a value object you should replace the object with a new one and not be allowed to update the values of the value object itself - updatable value objects lead to aliasing problems.

Community
  • 1
  • 1
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • I have not used hashmap, when considered values, Considering str1 is way different from str. whats happening to str1 will be different to str. But why the both return the same hashcodes. I got the hashcode never depends on references but why the strings returning the same hashcode when both of them are different. Can you please let me know ignoring hashmap – niks Jan 29 '16 at 15:18
  • @niks: what two strings are different? seems like your question needs a better example. – Nathan Hughes Jan 29 '16 at 15:21
  • I am considering str and str1 are different objects. But now I am clear that as the object values are same, when seen from the equals method, they will return the same hashcode, thats whats happening with my example – niks Jan 29 '16 at 15:25
  • So Hash codes are not only used for buckets in hashmap but also all the objects in the heap? – niks Jan 29 '16 at 15:40
  • 1
    @niks: I'm not clear about what you're asking. java.lang.Object implements hashcode so every object in the jvm has one. – Nathan Hughes Jan 29 '16 at 15:59
1

The hashCode() method of objects is used when you insert them into a HashTable, HashMap or HashSet. Just as important is the equals() method.

If two objects are equal, like you have demonstrated in your example (Two strings with the value 'Niks') (see this answer for more information) then they will have the same Hash, an important point to note is that just because two Objects have the same hash, they may not be equal!

Community
  • 1
  • 1
DominicEU
  • 3,585
  • 2
  • 21
  • 32