Hi, well i am unable to understand that why it is mandatory
to have same hashcode, if two objects are same?

Thanks in advance.

Answer Posted / rajshekaran pillai

As the JVM assigns the hashcode to an object value and not
the reference the hashcode for the same object will be same.
the equals() method of the object class uses this hashcode
to evaluate if the objects have same value. therefore

Object o1 = new String("ABC");
Object o2 = "ABC";

/**
* will have the same hashcode
* therefore when we do..
*/
if(o1 == o2){
System.out.println("The object reference is same");
}else if(o1.equals(o2)){
System.out.println("The object has same value");
}

/**
* The output is
* The object has same value
*/

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How do you use substring in java?

781


What is the difference between static method and instance method in Java?

828


Are static members inherited to sub classes?

895


What does int argc char * argv [] mean?

736


Can a lock be acquired on a class in java programming?

737


Which data type is a class in java?

778


What is meant by class and object in java?

738


What is the disadvantage of synchronization?

781


What are the two environment variables that must be set in order to run any java programs?

731


Is a char always 1 byte?

736


What is thread start?

724


What is private public protected in java?

815


What class allows you to read objects directly from a stream in java programming?

769


How do you allocate memory to object?

737


What is the difference between integer parseint and integer valueof?

770