One is called hashCode, the other equals.
One doesn't take any arguments, the other a single Object argument.
One returns an int, the other a boolean.
Should I go on?
What I'm trying to point out is that they are very different methods. They are linked though because of how they are used in hash maps (among others). These first use the hash code to find a bucket for a key, then use equals inside that bucket. If they aren't linked properly (as specified in the Javadoc page of Object) then this won't work.
Mark Guo wrote:What's the difference between hashcode() and equals() methods?
Literally, what the others have said is absolutely right; and I'd also suggest that you look at the API docs for Object.
Logically, they should be as similar as possible, given any other constraints you might have, such as time. That is to say that equal hashcodes should strongly indicate equal objects; however, a near-perfect hashCode() method won't be much use if it takes 10 seconds to run.
In addition to the other suggestions, Effective Java has a very good chapter on hashcodes in general, and the requirements of equals() and hashCode() as used together.
Isn't it funny how there's always time and money enough to do it WRONG?
hashcode() - Returns a hash code value for the object.
equals() - Indicates whether some other object is "equal to" this one.
The combined functionality of these two will we seen if we consider the example of hash code based collections, in that case while looking searching for the data in the collection firstly hashcode() will be called, which will narrow the scope to find that element and then it will call equals() method which will further help in finding that particular element in that scope.
Lets take a simple example - Say there are 5 buckets with many elements and the element you want to find has the probability to be in any of the bucket, then in that case your hascode() method will tell you the bucket and then it will call the equals() method to find your requested element in that bucket.
It's important to be precise about these things. If you would implement a hashcode() with lower-case c method in your class, it is not overriding the hashCode() method from class Object and your program is going to have a bug - your hashcode() method is never going to be called by hash-based collections such as HashMap and HashSet.
Java has something to prevent this bug: the @Override annotation. If you use this and you make the mistake mentioned above, the compiler will give you an error.