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
Difference between final and effectively final ?
What is time complexity java?
How many types of interfaces are there?
How hashset works internally in java?
What is difference between char array and string?
What is the advantage of OOP in java?
What are the limitations of procedural programming approach?
Do loops java?
What about main() method in java ?
What is your platform?s default character encoding and how to know this?
How do I start learning java?
Who developed java?
Can we force garbage collector to run ?
What is a class in java?
How many JVMs can run on a single machine and what is the Just-In-Time(JIT) compiler?