Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Hashcode doubt

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
subhadeep chakraborty
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 607
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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!
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 3626
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 70266
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving thread as too difficult for "beginning".
 
subhadeep chakraborty
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry, I understand
Just wanted to understand in which scenario would the default hashcode() method be useful ?

Thank you,
Subhadeep
 
Campbell Ritchie
Marshal
Posts: 70266
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 70266
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3626
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic