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

What is a conditional statement explain with example?

763


Distinguish between a predicate and a function?

771


What is xslt in java?

729


How do you make an arraylist empty in java?

693


What are the access modifiers available in java?

810


What do you mean by formatting?

741


What is the purpose of the system class in java programming?

753


What is meant by method overriding?

784


Why webdriver is an interface?

786


What is static import in java?

724


What are decalarations?

847


String class is defined under which package in java?

814


What is the function of static in java?

763


What is qms certification?

715


Can we create our own wrapper class in java?

743