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
Explain wait(), notify() and notifyall() methods of object class ?
What are different types of expressions?
Is oracle charging for java?
What is use of map in java?
What is the difference amongst jvm spec, jvm implementation, jvm runtime ?
What is a cup of java?
Objects or references which of them gets garbage collected?
Explain the difference between protected and default access.
Give us the name of the list layoutmanagers in java?
What are untrusted applets?
What is string intern in java?
What are assembly attributes?
Explain the difference between map and flatmap stream operation?
What is the purpose of using bufferedinputstream and bufferedoutputstream classes?
What are the approaches that you will follow for making a program very efficient?