-2

For an object of class CheckToString, when I give the object or the objectName.toString() as an argument to System.out.println() ie try to print it, i get the output as follows:

CheckToString@19821f

Here, the text after '@' is the hashCode, what all does it comprise of and what is the best practice of overriding hashCode()?

Sachin Verma
  • 3,712
  • 10
  • 41
  • 74
  • See http://stackoverflow.com/questions/13860194/what-is-an-internal-address-in-java/13860488#13860488 – NPE Oct 21 '13 at 09:23
  • A hashcode is any 32 bit integer that should be distinct for different objects (as reported by `equals(Object)`). – Tadas S Oct 21 '13 at 09:23

5 Answers5

0

what all does it comprise of

It comprises "the unsigned hexadecimal representation of the hash code of the object", as it says in the Javadoc.

does it contains memory address of the object

No. Also documented.

and what is the best practice of overriding hashCode()?

This is also documented clearly in the Javadoc.

I suggest you read it. It would be otiose to repeat it all here.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

This is there in the Java docs

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

You will find this link useful http://www.jusfortechies.com/java/core-java/hashcode.php

Pratik Shelar
  • 3,154
  • 7
  • 31
  • 51
0

Going by the spec, there is no relationship between hashcode and memory location. However a memory address of an object could be a good (if not better) value for a hashcode.

The general contract of hashCode is:

Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

Source

Dhana Krishnasamy
  • 2,126
  • 17
  • 36
0

As the Javadoc specify. hashcode is nothing but integer converted internal address.

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

http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode()

Abhijith Nagarajan
  • 3,865
  • 18
  • 23
-1

In java.lang.Object's hashcode it basically returns the memory address.(32 bit hexadecimal).

what is the best practice of overriding hashCode()?

When you start using collections then there would be time you want to match one object to another. As you may know two different objects can't be same but by overriding hashcode() and equals() you can make comparison possible.

Sachin Verma
  • 3,712
  • 10
  • 41
  • 74
  • The idea that hashCode uses the memory address is a historical artefact http://stackoverflow.com/questions/36236615/does-object-tostring-or-object-hashcode-ever-give-the-memory-address-of-the-obje – Raedwald Mar 26 '16 at 17:03