Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between Object reference and Object hash code

Tags:

java

whats the difference between an Object's reference and the same object's hash code value in java ?

like image 945
selvam Avatar asked Dec 31 '10 07:12

selvam


People also ask

What is object hash code?

Java Object hashCode() is a native method and returns the integer hash code value of the object. The general contract of hashCode() method is: Multiple invocations of hashCode() should return the same integer value, unless the object property is modified that is being used in the equals() method.

What is object and object reference?

When you create an object of a class as − Student obj = new Student(); The objects are created in the heap area and, the reference obj just points out to the object of the Student class in the heap, i.e. it just holds the memory address of the object (in the heap).

What is meant by object reference?

An object reference is information on how to find a particular object. The object is a chunk of main memory; a reference to the object is a way to get to that chunk of memory. The variable str does not actually contain the object, but contains information about where the object is.

Are hashes the same as objects?

Often we compare hashes in Ruby to Objects in JavaScript. Although they share some similarities, the functionality of both are not the same. The similarities stem from the fact that Ruby's hash and JavaScript's object appear to look the same. Lets take a look at how each are created with their seemingly similar syntax.


3 Answers

They are completely two different concepts.

Cat oldCat = new Cat();
Cat newCat = new Cat();
Cat oldCatRef = oldCat;

In the above example, oldCat and oldCatRef are references to the same object. Since they refer to the same object, their hashcodes will be equal.

But oldCat and newCat do not refer to the same object. They are references to two different objects. But they might have the same hashCode based on their implementation. hashCode is simply a method in Object class which you can override.

EDIT (from PeterJ): According to the JavaSE6 Object specification, if oldCat.equals(newCat) then the hashcode of the two should be equal. It's good programming to obey by that contract

You probably want to check the answers for this question as well:

difference between hash code and reference or address of an object?

like image 172
rkg Avatar answered Oct 23 '22 17:10

rkg


A reference to an Object is just that. A reference to an Object.

An Object's hashcode is the result of the hashCode() method which depending on implementation may be various things. The default hashCode():

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

like image 32
cherouvim Avatar answered Oct 23 '22 16:10

cherouvim


Two different Objects can have same hashCode. A reference is a unique pointer to an object where hashCode is a convenient computed attribute.

like image 3
fastcodejava Avatar answered Oct 23 '22 17:10

fastcodejava