3

First, I'm speaking about the default hashCode() method here and not something overridden. When we create a new object, 'new' operator returns the memory address of that object it creates; which in java we say it returns reference more generally. What I want to know is, is this the same as the value returned by the hashCode()?

What I BELIEVE is they are same. But then again, when we have more than 2^32 objects and given hashCode() returns an integer (2^32 different numbers) there will be collisions all over and when we pass objects it would be a real mess. How does JVM deal with?

5 Answers 5

5

When we create a new object, 'new' operator returns the memory address of that object it creates; which in java we say it returns reference more generally.

Well, a reference certainly doesn't have to be an address. It's a way of referring to an object - the exact value of the bits depends on the JVM implementation.

What I want to know is, is this the same as the value returned by the hashCode()?

Not necessarily. You certainly shouldn't try to make any assumptions on that front. Again, it's an implementation detail.

It's worth remembering that while the garbage collector can move objects around in memory (updating references as it goes), the hash code must not change based on this, which is an argument against your suggestion.

But then again, when we have more than 2^32 objects and given hashCode() returns an integer (2^32 different numbers) there will be collisions all over and when we pass objects it would be a real mess.

There will be hash code collisions, yes. That's unavoidable - anything using hash codes needs to take the possibility of collisions into account. There can't be reference collisions though - a JVM which is able to support more than 232 values concurrently clearly can't just make the hash code and the reference have the same value.

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

6 Comments

There I was, editing my post to something coherent and readable and a new answer comes up. Thanks for saving me the time to edit ;)
Why even this uncertainty in the javadoc for the hashcode like "As much as is reasonably practical, the hashCode method....". "As much as reasonably practical", can they probably talking about when there are more than 2^32 objects exists?
@user601L: Not just that - there may be other reasons for it to be impractical.
@user601L: Well if there are 2^32-1 hash codes already in use, how long do you really want the JVM to spend trying to find the last one? How much memory do you want it to spend just trying to keep this quick?
Thank you. I have one more clarification. Does java keep track of all the objects being passed using some kind of a mechanism to avoid reference collisions? I mean, We just see only a value is being passed as the object but internally java is doing some extra work. something like that?
|
2

which in java we say it returns reference more generally. What I want to know is, is this the same as the value returned by the hashCode()?

From the Object#HashCode (my highlight):

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.)

So this is opaque to you the programmer.
All you need to know is that by new you get a handler for an object and that a hashCode is a "recipe" based on the conversion of the internal address to an integer representing the hashCode for an object, that is the same regardless of the memory area your object currently lies (e.g. due to GC movements)

Comments

1

Since hash codes do not need to be unique, there would be no problem if converting addresses of two objects in 64-bit space resulted in hash code collision: there is no requirement for hash codes of different objects to be different - only a requirement of hash codes of equal objects to be the same.

In Real LifeTM the default hashCode values do look suspiciously similar to the object's address. However, this information is of no use to Java programmers: even if hash codes were equal to object's addresses, Java does not provide a mechanism to exploit this knowledge, rendering it a useless implementation detail.

2 Comments

Real Life, why TM? just curious
@user601L Since different people understand "Real Life" differently, there's no such thing as "the real life". Therefore my words shouldn't be taken too seriously, because they are nothing more than a hasty generalization of my own experience.
0

new in Java does not return the memory address. And even more: Java can (and does) relocate objects in memory. So although the hashCode() implementation in Java could return a memory address, it probably doesn't as it wouldn't be stable over the lifetime of an object.

The hashCode does not have to be unique. In cases where the hashCode is used for identifying objects (eg in HashMap), it needs to be paired with an implementation of equals() to resolve collisions.

Comments

0

When we create a new object, 'new' operator returns the memory address of that object it creates

No. It returns a Java reference. Because of garbage collection and the implication that objects can be moved, the one thing you can bet on is that it isn't a memory address.

is this the same as the value returned by the hashCode()?

No. A hashCode is not a memory address, especially if hashCode() has been overridden.

What I BELIEVE is they are same.

Your belief is baseless.

But then again, when we have more than 2^32 objects and given hashCode() returns an integer (2^32 different numbers) there will be collisions all over and when we pass objects it would be a real mess. How does JVM deal with?

So obviously your belief is mistaken. Consider also 64-bit implementations. The only problem here is your assumptions.

1 Comment

1)The OP asks specifically about the hashCode of Object. 2) The hashCode is an int which is always a 32 bit value regardless if the JVM is 32 or 64 bits

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.