Object comparison hashCode() vs equals() Java

Object comparison hashCode() vs equals() Java


hashCode() vs equals(): About Hash code

hashCode() is a method of Object class. Hash code is an integer representation of an object by JVM. Hash code is system generated using some formula (let us not go much of detais of formula). Hash codes generated need not be the same for different times of execution. Internally, the objects are stored in Hashtable format. The equals() method, as we know earlier, compares two strings or characters. For comparison, the JVM uses their hash codes only. This way of comparison is very faster than comparing each character in the two strings. This hash code format cannot be used by the programmer in coding.

hashCode() vs equals(): About equals() method()

equals() is a method of Object class. This method is not designed to exactly compare the properties of two objects; but designed, to know whether two references refer the same object or not. Many classes override this method for their convenience. String() and System classes has overridden this method to know two objects are same or not.

Following program shows the usage of hashCode() vs equals() methods.


hashCode() vs equals()
Output screenshot onhashCode() vs equals() Example

getClass() method of Object class returns the class for which object t1 belongs. hashCode() method returns the hash code of the object t1. equals() method for t1 and t2 returned false and for t3 and t4 returned true as t3 and t4 refer the same t1 object. Comparing objects with hashcode is known as hashing.

About Hashing and Hashcode

Comparing two strings letter by letter in a for loop is a time taking process. To make faster, the JVM converts each string into an integer number called hashcode. Different strings with different sequence of characters have different hashcodes. Comparison with integer numbers gives maximum performance. Usage of hashcode numbers for comparison, searching of duplicate elements and identification is faster.

Hashing is process of converting a string or object into a 32-bit hashcode integer number. Two objects are said to be equal if their hashcodes are same. hashCode() is used in combination with equals() method. When compared, hashing is done automatically by the JVM. Hashing, in data structures, is done implicitly in the basic operations with add(), contains(), remove() and size() etc. Hashing is more useful to compare the sets of large content.

View All java.lang Examples

8 thoughts on “Object comparison hashCode() vs equals() Java

  1. Kristi Relan

    Hello sir,
    i am a bit confused equals() method is actually used to compare the values so how it is comparing hash code in this. Please explain

    1. S. Nageswara Rao, Corporate Trainer

      public class Demo
      {
      public static void main(String[] args)
      {
      String s1 = “hello”;
      String s2 = “hello”;
      String s3 = “world”;

      System.out.println(“s1 hashcode: ” + s1.hashCode());
      System.out.println(“s2 hashcode: ” + s2.hashCode());
      System.out.println(“s3 hashcode: ” + s3.hashCode());
      }
      }

Leave a Reply

Your email address will not be published. Required fields are marked *