What is hashCode?

What is hashCode?

The hashCode() is id number allocated to an object by JVM. Objects in Java have hash codes associated with them. An object\'s hash code is a signed number that identifies the object (for example, an instance of the parent class). An object\'s hash code may be obtained by using the object\'s hashCode() method as follows:
int hashCode = SomeObject.hashCode();

The method hashCode() is defined in the Object class and is inherited by all Java objects. In order for the Java Collections to work properly (and everything else in Java), the equals() and hashCode() methods must be compatible. Here, compatible means that if equals() reports that two instances are the same, then the hashCode() of both instances must be the same value. 

Date:2015-01-09 00:00:00

Post Your Answers

User Email:

User Name:

Related Java Subjective Links

Java Subjective interview questions and answers for experienced and fresher