gabriel angelos
gabriel angelos

Reputation: 359

hashCode() purpose in Java

I read in a book that hashCode() shows a memory area which helps (e.g. HashSets) to locate appropriate objects in memory. But how can that be true if we cannot manipulate memory in Java directly? There are no pointers, in addition to it objects are created and moved from one place to another and the developer doesn't know about it.

I read that realization like hashCode() {return 42;} is awful and terrible, but what's the difference if we can't instruct VM where to put our objects?

The question is: what is the purpose of hashCode() on deep level if we can't manipulate memory?

Upvotes: 8

Views: 8521

Answers (7)

Johnny
Johnny

Reputation: 15423

The hashCode() function takes an object and outputs a numeric value, which doesn't have to be unique. The hashcode for an object is always the same if the object doesn't change.

The value returned by hashCode() is the object's hash code, which is the object's memory address in hexadecimal.

By definition, if two objects are equal, their hash code must also be equal. If you override the equals() method, you change the way two objects are equated and Object's implementation of hashCode() is no longer valid. Therefore, if you override the equals() method, you must also override the hashCode() method as well.

For more, check out this Java hashcode article.

enter image description here

Upvotes: 0

Nathan Hughes
Nathan Hughes

Reputation: 96444

I like Jon Skeet's answer (+1) but it requires knowing how hash tables work. A hash table is a data structure, basically an array of buckets, that uses the hashcode of the key to decide which bucket to stick that entry in. That way future calls to retrieve whatever's at that key don't have to sift through the whole list of things stored in the hashtable, the hashtable can calculate the hashcode for the key, then go straight to the matching bucket and look there. The hashcode has to be something that can be calculated quickly, and you'd rather it was unique but if it isn't it's not a disaster, except in the worst case (your return 42;), which is bad because everything ends up in the same bucket and you're back to sifting through everything.

The default value for Object#hashCode may be based on something like a memory location just because it's a convenient sort-of-random number, but as the object is shunted around during memory management that value is cached and nobody cares anyway. The hashcodes created by different objects, like String or BigDecimal, certainly have nothing to do with memory. It's just a number that is quickly generated and that you hope is unique more often than not.

Upvotes: 8

asgs
asgs

Reputation: 3984

Yes, it has got nothing to do with the memory address, though it's typically implemented by converting the internal address of the object. The following statement found in the Object's hashCode() method makes it clear that the implementation is not forced to do it.

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.

Upvotes: 0

Miquel
Miquel

Reputation: 15675

A hashcode is a function that takes an object and outputs a numeric value. The hashcode for an object is always the same if the object doesn't change.

Functions like hashmaps that need to store objects, will use a hashcode modulo the size of their internal array to choose in what "memory position" (i.e. array position) to store the object.

There are some cases where collisions may occur (two objects end up with the same hashcode, and that, of course, need to be solved carefully). For the details, I would suggest a read of the wikipedia hashmap entry

Upvotes: 1

Vuk Vasić
Vuk Vasić

Reputation: 1418

HashCode is a encryption of an object and with that encryption java knows if the two of the objects for example in collections are the same or different . (SortedSet for an example)

I would recommend you read this article.

Upvotes: 0

paulsm4
paulsm4

Reputation: 121849

A hash code is a just a "value". It has nothing more to do with "where you put it in memory" than "MyClass obj = new MyClass()" has to do with where "obj" is placed in memory.

So what is a Java hashCode() all about?

Here is a good discussion on the subject:

K&B says that the hashcode() contract are :

  1. If two objects are equal according to the equals(Object) method, then calling the hashCode() method on each of the two objects must produce the same integer result.

  2. If two objects are unequal according to the equals(Object) method, there's no requirement about hashcode().

  3. If calling hashcode() on two objects produce different integer result, then both of them must be unequal according to the equals(Object).

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503200

I read in a book that hashCode() shows a memory area which helps (e.g. HashSets) to locate appropriate objects in memory.

No, that's a completely bogus description of the purpose of hashCode. It's used to find potentially equal objects in an efficient manner. It's got nothing to do with the location of the object in memory.

The idea is that if you've got something like a HashMap, you want to find a matching key quickly when you do a lookup. So you first check the requested key's hash code, and then you can really efficiently find all the keys in your map with that hash code. You can then check each of those (and only those) candidate keys for equality against the requested key.

See the Wikipedia article on hash tables for more information.

Upvotes: 10

Related Questions