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 is a Transient Object?
What is codebase?
Is empty in java?
How do you declare an array in java?
Can we convert integer to string in java?
What is multi-catch block in java?
What is a char in java?
What is the use of bufferedreader?
What is an exception? difference between Checked and Unchecked exception in Java
How do you write a scanner class in java?
What does @param args mean in java?
FOR EXAMPLE WE R HAVING TWO LIST ELEMENTS ..BOTH LISTS CONTAINS ID,NAME,PLACE ..I NEED TO COMPARE BOTH IDS IN TWO LISTS,IF ID'S R SAME MEANS WE HAVE ADD THE DETAILS(LIKE NAME,PLACE) TO MAP...HOW IS POSSIBLE ?CAN ANY ONE SUGGEST?
Why there is no call by reference in java?
What is final method in java?
How are the elements of a gridbaglayout organized in java programming?