Way2Java

Differences between Hashcode and Equals in Java

Hashcode Equals

Both are methods of java.lang.Object class. Hashcode is an integer representation of the objects of an array, String and a DS. Hashcode increases the performance of comparing these three. For example. to compare two long strings with many number of characters, writing a for loop and comparing each character takes a very good time. Java adopts hashcode technique to compare. That is, both strings are converted into integer values (known as hashcode) by Java and then these two integer values are compared. If both integers (compared with ==) are equal, then the two strings contain the same characters in the same order.

Both these methods are illustrated in Object comparison – hashCode() & equals().