hashCode() computation

Hi All,

How does the hashCode() of Object class compute the hashcode?

I went through the documentation. All it says is :

"(This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java programming language.)" And the method is a native method, so I couldn't even look at the code.

Please let me know if anyone has any idea about it.

Thanks,

Anushka

[492 byte] By [Anushka_Shaha] at [2007-11-27 9:12:24]
# 1
> > Please let me know if anyone has any idea about it.> Why do you think you need to know how it is generated?
sabre150a at 2007-7-12 21:58:52 > top of Java-index,Java Essentials,Java Programming...
# 2

Hi,

I am working on utility that checks if data in a file is intact after renaming operation on File. (One of those weird client requests!) . If hashcode is generated using the content of the Object (file in my case), then it will be same if data in the file is intact after renaming operation. But if hashcode is generated using some other property of an Object, then it doesnt guarentee the unique hashcode for each object. I just want to know the computation method to assure that the hashcode generated will be unique for each of the File Object.

Thanks

Anushka_Shaha at 2007-7-12 21:58:52 > top of Java-index,Java Essentials,Java Programming...
# 3

A File object in no way represents the content of the file and a File object does not even have to point to real file. The hash code of a File object in no way represents the hash of the content of the file even if the File object actually points to a real file.

For this task I use SHA1 hash of the content using the MessageDigest class.

Message was edited by:

sabre150

sabre150a at 2007-7-12 21:58:52 > top of Java-index,Java Essentials,Java Programming...
# 4

Hashcode in Java is used to establish uniqueness of an object and to identify multiple object names which actually reference the same object.

The implementation is such that an object will always have a unique id or number distinct from any other object.

Java provides one of several methods of using internal address of the object for this purpose. This need not be the only way. You can go ahead and override java.lang.Object.hashCode() and implement it in your own way.

java_a at 2007-7-12 21:58:52 > top of Java-index,Java Essentials,Java Programming...
# 5

> Hashcode in Java is used to establish uniqueness of

> an object and to identify multiple object names which

> actually reference the same object.

Wrong - hash codes are not unique.

>

> The implementation is such that an object will always

> have a unique id or number distinct from any other

> object.

Wrong - hash codes are not unique.

Run this code -public class TwoStringWithTheSameHashCode

{

public static void main(String[] args)

{

int hash1 = "ABCDEa123abc".hashCode();

int hash2 = "ABCDFB123abc".hashCode();

System.out.println(hash1 + " " + hash2);

}

}

Message was edited by:

sabre150

sabre150a at 2007-7-12 21:58:52 > top of Java-index,Java Essentials,Java Programming...
# 6
You are right. Hashcodes need not be unique.Thanks Sabre !
java_a at 2007-7-12 21:58:52 > top of Java-index,Java Essentials,Java Programming...