answersLogoWhite

0

The hashCode method is used to create a unique identification number to describe the state and type of an object.

The Java API description of the method is:

Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the Object.equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

What is the difference between reference and hashcode?

There is no relation between reference and hascode, Java reference is unique pointer which refers an object. so each object will have a unique reference. but 2 diff object can have same hashcode.


Why must you override the hashcode method while overriding the equals method?

Because, if two objects are supposed to be equal as per the equals() method, then the value returned by the hashCode() method must also be the same. This will not be the case if you override only the equals method and this can have some confusing effects when using those objects with hash related collections. So it is always a good idea to override the hashCode() method if you are providing an implementation for the equals method.


What is the difference between reference and hash code in java?

Cat oldCat = new Cat(); Cat newCat = new Cat(); Cat oldCatRef = oldCat; In the above example, oldCat and oldCatRef are references to the same object. Since they refer to the same object, their hashcodes will be equal. But oldCat and newCat do not refer to the same object. They are references to two different objects. But they might have the same hashCode based on their implementation. hashCode is simply a method in Object class which you can override.


How many methods does the Object class have?

The Object class has 11 methods (plus the constructor). They are: clone(), equals(), finalize(), getClass(), hashCode(), notify(), notifyAll(), toString(), wait(), wait(long timeout), wait(long timeout, int nanos).


What is hashcode in java?

hashcode is an integer number which is provide to each object by jvm note that this is not address of object but for convencing internally they use Java uses the hash function given below:- s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation.


Static method can be call object?

yes we can call a static method with object


What is the main purpose of the scientific method?

The main purpose of the scientific method is to test ideas


What is the name of the method of finding the volume of an object with an irregular shape?

The method of finding the volume of an object with an irregular shape is called displacement method or water displacement method. This method involves submerging the irregular object in water and measuring the volume of water displaced to determine the object's volume.


What is modified conventional method in baking?

object Object]What


What is by purpose?

A by-purpose is an indirect or incidental purpose for an object or an action.


What is a by-purpose?

A by-purpose is an indirect or incidental purpose for an object or an action.


What kind object serves no purpose?

No object serves no purpose, though objects serve a purpose when they are needed. An object that serves no purpose is one that is not needed at the time. Synonyms for something that serves no purpose are superfluous, unnecessary, needless, or excrescent.