Showing posts with label equals() in Eclipse. Show all posts
Showing posts with label equals() in Eclipse. Show all posts

Wednesday, 21 March 2012

Overriding hashCode() method in Java or Why always override hashcode() if overriding equals()?

Object class has following five non final methods.

1. clone()
2. equals(Object obj)
3. finalize()
4. hashCode()
5. toString()

Every Java class has Object as a superclass so by default all above methods are provided in every java class. In this article we will discuss in detail the concepts of hashCode() method. We will discuss why it is necessary to override hashCode method,Why always override hashCode() if overriding equals() and how we can override them.

Default implementation of hashCode() in Object class

The signature of hashCode is:

public int hashCode()