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
Give me example of derived data types.
What are different types of arrays?
What does pointer mean?
which pattern is default in scanner package?
How do you remove an object from an arraylist in java?
List primitive java types?
What is a vector in java?
What are the steps that are followed when two computers connect through tcp?
When should I use singleton?
Is it compulsory for a try block to be followed by a catch block in java for exception handling?
Do you know why doesn't the java library use a randomized version of quicksort?
What does ide stand for?
How do you convert boolean to boolean?
What is the final class modifier?
What is illegal identifier in java?