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
In case of inheritance what is the execution order of constructor and destructor?
Can we override the static method?
Can private class be extended java?
Can we create a class inside a class in java?
Can you extend main method in java?
Which of the following is not an isolation level in the JDBC
What is the protected method modifier?
Break statement can be used as labels in java?
For class CFoo { }; what default methods will the compiler generate for you>?
How many techniques can be employed to create a string object?
Why we cannot override static method?
Do we need to manually write Copy Constructor?
What are the 6 boolean operators?
Explain about abstract classes in java?
What is garbage collection? What is the process that is responsible for doing that in java?