1

I know that the hashmap keys should be immutable objects or at least have a consistent hashcode to properly retrieve its value from the Map/Set. But if I use a mutating object as key without overriding hashcode or equals, will its hashcode be consistent thought its life cycle. I have tried it to retrive hashcode of a mutating object and have found it consistent always.

1

4 Answers 4

4

The contract for hashcode states:

Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified.

So there is no guarantee that the hashcode won't change if you mutate your object. It happens that some JDK implementations might use some sort of internal address but they don't have to so you should not rely on that.

Sign up to request clarification or add additional context in comments.

5 Comments

In this answer - stackoverflow.com/a/11807339/4691279 ... if this is true that memory address from initial allocation is entered into object header and hashCode always returns the same, then isn't it possible that two objects can end up with same hashCode() value? If you could please throw some light on this..
Hashcodes are not unique - for example a hashcode method that return 1; is valid. You may want to search for "hashcode collision", there are several Q&A on Stackoverflow, e.g. stackoverflow.com/q/4360035/829571
Thanks for your reply.. yes, that I know.. my point is that suppose hashCode() method is not overriden.. and an object o1 get allocated at memory location abc123.. so when hashcode() is called on it then abc123 will be returned and this will always be true.. and as per stackoverflow.com/a/11807339/4691279 answer this will always be the case because initial memory address is written in the header.. now, suppose that object o1 gets moved to old generation.. and another new object o2 gets created at abc123.. and here as well since hashcode() is not overriden so we will get abc123.. [continued...]
so essentially two objects returning same hashCode.. please note that this is possible and also know about hashcode collision.. I know the hyothetical scenario mentioned in my above comment can be true or not..
What you describe is probably true for hotspot. But this is not specified by the JVM specifications. It means that another JVM may use different algorithms: what you are saying may be true on hotspot and false on IBM JVM for example. Feel free to ask a separate question if you want more details.
3

The default hash code of the object will not change over time in the latest version of the HotSpot JVM. In fact, the hash code is derived from the initial allocation address and entered in the object header (see JVM Whitepaper) as the object is moved by the garbage collector and will not change afterwards.

Comments

0

The default Java implementation of hashcode is based on the pointer to the object, so it should not change when instance variables are changed.

Edit

But Martijn beat me to it.

Comments

0

Yes, it will be constant if you don't override the hashCode method. The default hashCode is the original location of the object in memory. This memory location is retrieved by this method:

System.identityHashCode(Object)

Check out this thread on StackOverflow:

How does the JVM ensure that System.identityHashCode() will never change?

4 Comments

It's not the current location of the object in memory - after all, that can change over time as the garbage collector moves things around.
I'm not sure, to be honest. I've never looked at the implementation details around it :(
"The default hashCode is the original location of the object in memory." - not necessarily. See for example: this previous hotspot implementation which adds some randomness. Just checked open JDK 7 and it looks similar.
java doesn't gurantee that it will be constant, it depends on implementation of jvm.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.