wood burning stoves*
The moose likes Java in General and the fly likes Hashcode doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Hashcode doubt" Watch "Hashcode doubt" New topic
Author

Hashcode doubt

subhadeep chakraborty
Ranch Hand

Joined: Jul 29, 2006
Posts: 67
Hi,
I have a doubt regarding hashcode.As far as I know, hashcode method of Object class returns the address of the object on the heap.

For eg. When i have
A a = new A();
can i say
data stored in a is equal to the hashcode of A i.e the address of Object A.

Thanks in advance,

Regards,
Subhadeep
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18824
    
  40


The Object class returns the identity hashcode as the hashcode... this may or may not be related to the address of the object at the time that it is instantiated.

Regardless, the identity hashcode of the object will never change. And the address of the object may change many times during it lifetime, as it is being moved around by the garbage collector.

So...

subhadeep chakraborty wrote:
can i say data stored in a is equal to the hashcode of A i.e the address of Object A.


When the object is instantiated? Maybe. It depends on the implementation.

Later? No. The hashcode return from the Object is definitely not the address.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
subhadeep chakraborty
Ranch Hand

Joined: Jul 29, 2006
Posts: 67
Thanks for helping me understand,

As I understand when I call hashcode() method for the first time, it returns the memory address
but subsequently calling hashcode() method is not sure to return the memory address because the object may be garbage
collected and Object's hashcode() method returns identity hashcode.

My doubt is:
If I have A a = new A();
Lets assume it garbage collected, not because it has gone out of scope but because I am not using it.(Is this possible ?)
And now I am using the same Object(a).
So, my doubt is whenever an Object is garbage collected and later reclaimed is it always a new Object getting created
because wheather it is the same object can only be identified by its hashcode() which returns the memory address and now it will most probably contain a new address.
In short, If an Object is garbage collected and later reclaimed, is it the same Object or a new Object.

Regards,
Subhadeep
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18824
    
  40

subhadeep chakraborty wrote:
As I understand when I call hashcode() method for the first time, it returns the memory address


Let me elaborate. When I say "maybe", I mean "no". I believe some implementation may use the address, but this is very unlikely to be the address, as there are other issues involved too -- so other items are used to calculate the identity hashcode too.

subhadeep chakraborty wrote:
but subsequently calling hashcode() method is not sure to return the memory address because the object may be garbage
collected and Object's hashcode() method returns identity hashcode.


Let's elaborate this too. The GC is involved with all objects -- not just objects that are garbage collected. So, you can have an object which you use a lot, it is *not* garbage collected, and the GC will move it around..... Objects move around. Even those that are in use.

Henry
subhadeep chakraborty
Ranch Hand

Joined: Jul 29, 2006
Posts: 67
Thank you so much, Henry
I am a begineer in the java.

" I am not able to find on what parameters does IdentityHashcode depend "
Could you please advice ?

Could you tell me where from can I download JDK source code ?
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

subhadeep chakraborty wrote:
" I am not able to find on what parameters does IdentityHashcode depend "
Could you please advice ?


What do you mean by Identity Hash code? Why do you really need to know that? I don't mean to say - you shouldn't ask question but the idea of using a language like Java is hiding behind abstraction - leave the lower level details to those who best need to worry about them.

Having said that I can say I honestly don't know the answer to your question

subhadeep chakraborty wrote:
Could you tell me where from can I download JDK source code ?


I use http://www.docjar.com to look up the source code for specific classes. Make sure to use fully qualified names while searching (e.g. java.lang.Object) or you could get loads of not related results!


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Sam Mercs wrote:

I use http://www.docjar.com to look up the source code for specific classes.


You could do that, I suppose, but note that the complete source for all the JDK classes comes with the JDK download in a file named "src.jar" -- so you probably already have the full source on your system.


[Jess in Action][AskingGoodQuestions]
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
Sam Mercs wrote:
subhadeep chakraborty wrote:
" I am not able to find on what parameters does IdentityHashcode depend "
Could you please advice ?

What do you mean by Identity Hash code? Why do you really need to know that?

Well, based on reading the thread above, it seems clear that Subhadeep is simply trying to understand Henry's comment - since Henry is the one who used the term. (Minus the unorthodox capitalization.)

Ernest Friedman-Hill wrote:
Sam Mercs wrote:
subhadeep chakraborty wrote:Could you tell me where from can I download JDK source code ?

I use http://www.docjar.com to look up the source code for specific classes. Make sure to use fully qualified names while searching (e.g. java.lang.Object) or you could get loads of not related results!

You could do that, I suppose, but note that the complete source for all the JDK classes comes with the JDK download in a file named "src.jar" -- so you probably already have the full source on your system.

Neither of these sources will be complete enough to answer Subhadeep's question here though, since Object's hashCode() method is declared as native, and native code is not included in src.jar. The same is true for System.identityHashCode(), which provides the same result. It's great to use src.jar as a resource in general, but it can't answer everything. There are other places you can find more complete code that would include native methods for the Object class - but I'm pretty sure Subhadeep would not benefit much from looking at this - it tends to be very complex an confusing to figure out. Not worth the effort in this case.

Subhadeep: when Henry refers to the identity hash code, he means the hashCode() in Object is based only on identity, not on any of the data fields that may be found associated with a given instance. If it helps, you can think of it as maybe being based on the original memory location assigned to the object - but not the same as that memory address.

Subhadeep wrote:My doubt is:
If I have A a = new A();
Lets assume it garbage collected, not because it has gone out of scope but because I am not using it.(Is this possible ?)
And now I am using the same Object(a).

No, this isn't possible. If there is any way for you to use that object again, the object can't be garbage collected.

Subhadeep wrote:So, my doubt is whenever an Object is garbage collected and later reclaimed is it always a new Object getting created
because wheather it is the same object can only be identified by its hashcode() which returns the memory address and now it will most probably contain a new address.
In short, If an Object is garbage collected and later reclaimed, is it the same Object or a new Object.

I'm not sure what you mean by "reclaimed" here. When an object is collected by GC, we may say it has been reclaimed by the collector, and its memory freed up. This means the object is gone, period. You seem to be using "reclaimed" to mean something different, like maybe "recreated" or "resurrected". That doesn't happen. So the question, I think, is meaningless.

As Henry notes, it is possible for objects to be moved in memory. This is different from garbage collection, and is not done by the garbage collector. And it's done in a way that you really shouldn't be able to observe any effect from the moving. (Well, unless you're somehow hacking the JVM. Or possibly it might effect timing in some cases, but it's going to be very hard to observe this.) This really isn't something you should ever need to think about.
subhadeep chakraborty
Ranch Hand

Joined: Jul 29, 2006
Posts: 67
Thanks for that link Sam,
Thanks Hill, I will look into src.jar
Thanks Mike, I have a question for you.


when Henry refers to the identity hash code, he means the hashCode() in Object is based only on identity


Every Object has an identity which is returned by the constructor, as mentioned by "Grady Booch" in one of his books, I think you are talking about the same thing.I am not able to understand the word "identity"(how do you define identity ?).Could you please elaborate a bit.

Thanks again,
Subhadeep
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18824
    
  40

subhadeep chakraborty wrote:
when Henry refers to the identity hash code, he means the hashCode() in Object is based only on identity


Every Object has an identity which is returned by the constructor, as mentioned by "Grady Booch" in one of his books, I think you are talking about the same thing.I am not able to understand the word "identity"(how do you define identity ?).Could you please elaborate a bit.


You may be reading too much into what I am saying... when I refer to the "identity hash code", I am referring to the value returned from the Object.hashCode() method. This is the same terminology used in Sun's documentations.

Henry
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38756
    
  23
Moving thread as too difficult for "beginning".
subhadeep chakraborty
Ranch Hand

Joined: Jul 29, 2006
Posts: 67
Thanks Henry, I understand
Just wanted to understand in which scenario would the default hashcode() method be useful ?

Thank you,
Subhadeep
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38756
    
  23
Have a look at the equals() and hashCode() methods of Object. You would find the default implementation useful whenever you use the default implementation of equals().
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

IdentityHashMap uses the System.identityHashCode which returns the hashcode returned by the default method hashCode(). For maps contain large number of objects with expensive equals and hashCode methods, IdentityHashMap perform much better since none of these methods are used.


http://muhammadkhojaye.blogspot.com/
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38756
    
  23
Expensive . . . hashCode() methods?

If you are using something as a Key in a Map you want it to be immutable, so you can use lazy evaluation and/or caching for the hash code, and there will be no need for an expensive hashCode() method.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
I can imagine some cases where hashCode() may be expensive even with caching. But that's a poor reason to prefer IdentityHashMap over a regular HashMap. Much more important is the fact that the two can have very different behavior. Use IdentityHashMap if you want or need that different behavior, not just because you think it will be faster. It's pretty rare that you may need this behavior, but it can happen. The javadoc gives a few examples of legitimate uses.
subhadeep chakraborty
Ranch Hand

Joined: Jul 29, 2006
Posts: 67
Sorry for answering late. Thank you all for helping me out with the answers,
Just wanted to know is there any book which would give me a better insight into understanding concepts like this. I have read books like scjp1.6 Kathy Sierra, scjp 1.4 khalid Mughal, Complete Reference, Core Java(Vol 1) Sun Press and Effective Java.

Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hashcode doubt