Can some one please explain what application actually means in this hashcode() contract?

StackOverflow https://stackoverflow.com/questions/11536515

  •  21-06-2021
  •  | 
  •  

Question

From JLS:

Whenever it is invoked on the same object more than once during an execution of an 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.

What is meant by the term application here and why the hashcode() for on object is allowed to vary between different execution of the same application ?

Was it helpful?

Solution

Application is your Java program.

It may vary from different application instances, as it is by default (hashcode of Object) based on the address of the object.

OTHER TIPS

What is meant by the term application here

Its as simple as it sounds, no tricks here.

why the hashcode() is allowed to vary between different execution of the same application ?

Hash collections are in memory only and are re-built when your application restarts. This means it can have a different implementation each time its built. (It can't be change for an an existing collection)

The hashCode method is used by various java classes. For example, hashCode is used by HashMap to store and retrieve the keys efficiently.

In order to give reliable results the contract of hashCode is stated as is. E.g. if the hashCode of an object used in a HashMap was not the same during a run of your application, the HashMap would not be able to retrieve the key consistently.

So applications here means a running instance of your java program. I would be fine if the hashCode would return different values if the program would be stopped and started again.

Application means your Java program. hashcode may vary between different instances of your program, but not in the same instance.

For more on hashCode see

The purpose of hashcode() is to create a hashcode of an object so it can be stored in a hash set, hash map etc. The purpose of hashcode() is not comparing different objects (thats whats equals() is for).

It is simply not neccessary for hashcode() to produce always the same result for several runs of your JVM. Although most java objects always return the same hashcode for the same state of the instances you should never depend on it.

You should also note that if you override equals() in a class you should also override hashcode().

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)

By an example:

class Employee{
public int id;
public String name;

public Employee(int id,String name){
 this.id=id;
 this.name=name;
 }

public boolean equals(Employee employee){

return (this.id==emplooyee.id && this.name.equals(employee.name))

}

public hashcode(){

return id+name.hashcode();

}

Now assume following code in some other class being called by a main function;

 Employee emp = new Employee(1, "Sam");
 emp.hashcode();
 //some other code with does not modify the contents of `emp`
 emp.hashcode();

these calls to emp.hashcode() method will return the same integer even if called multiple times during the same execution.

This is what is meant by the statement:

Whenever it is invoked on the same object more than once during an execution of an application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified.

Now when you run the program again with say

Employee emp = new Employee(1, "Sam");

Here the emp.hashcode(); method may or may not return the same integer as previous one (depending on the hashcode() method implementation ).

This is what meant by the remainder statement:

This integer need not remain consistent from one execution of an application to another execution of the same application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top