In Java, a hash code is an integer value that is associated with each object. Its main purpose is to facilitate hashing in hash tables, which are used by data structures like HashMap. The hash code value of an object is returned by calling the hashCode() method on that object. This method is implemented by default in the Object class and is, therefore, inherited by user-defined classes as well. The hashCode() method returns the same integer value when called on the same object during the same instance of a Java application, provided that no data used by the equals() method is modified. When hashCode() is called on two separate objects that are equal according to the equals() method, it returns the same hash code value. However, if it is called on two unequal objects, it will not necessarily return different integer values.
The hash code is used for bucketing in hash implementations like HashMap, HashTable, HashSet, etc. The value received from hashCode() is used as the bucket number for storing elements of the set/map. This bucket number is the address of the element inside the set/map. When you do a search, the hash code is first obtained and then the bucket is searched which significantly reduces the search time.
In summary, a hash code in Java is an integer value associated with each object that is used to facilitate hashing in hash tables, which are used by data structures like HashMap. The hashCode() method is used to compute the hash values of Java objects. The hash code is used for bucketing in hash implementations like HashMap, HashTable, HashSet, etc.