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
What are multiple inheritances? Is it supported by java?
Why java is made?
Define interface in java?
What are constants and how to create constants in java?
What is mvc in java?
What is a java developer salary?
What are the various access specifiers in java?
How many tetrahedral voids are there in bcc?
Explain when noclassdeffounderror will be raised ?
What is string data type?
Why pointers are not used in java?
Which container method is used to cause a container to be laid out and redisplayed in java programming?
Which is better singleton or static class?
What are the changes in java.io in java 8 ?
Name few java.lang classes introduced with java 8 ?