why the equals method can be override?when we override the
equals method?
Answers were Sorted based on User's Feedback
equals method available in object class is for address
comparison not for content comparison.so for content
comparison we have to override equals method in our class.
wrapper classes and string class has overridden equals
method for content comparison.
| Is This Answer Correct ? | 11 Yes | 3 No |
Answer / dsureshkumar28
question is not clear
may ur question is
why hashcode() will be override when u override equals()?
is it
| Is This Answer Correct ? | 5 Yes | 1 No |
Answer / ganesh
We need to override the equals method when we write our own class and we want the instance of that class to be part of some collection.
When you place the object in collection and make the manipulation on them, say sorting/searching etc.
The comparison of the object has to be done by the alogorithm.
The Algorithm uses the equals method to check for equality of two objects. Otherwise it wont be having any means to check for equality.
class Student
{
private String serialNo;
private String name;
}
two student objects with the same name cannot be the same students, but two student objects with the same serial number should the same object.
search(Student st,List a)
{
Collections.binarySearch(a,st);
}
when you want to put the objects in the hashMap, then overriding the cashCode is necessary. HashMap calls the hashCode method on the object to insert into HashMap.
| Is This Answer Correct ? | 4 Yes | 0 No |
Answer / madhu
equals() method will be available in primodian class that is
Object class, but by default it compares only the addresses,
not the contents, So if you want the content comparison you
have to override equals(), but if u override the equals(),
you have to override the hashCode() also.
| Is This Answer Correct ? | 3 Yes | 2 No |
Answer / manju kt
to check if 2 objects references to the same memory
location..
| Is This Answer Correct ? | 1 Yes | 9 No |
How many decimal digits is 64 bit?
Can we have a method name same as class name in java?
What is the importance of finally block in exception handling?
Which class should you use to obtain design information about an object in java programming?
Which class contains a method: cloneable or object?
Explain the inheritance?
what is check p object in java
What is constructor and virtual function? Can we call a virtual function in a constructor?
My application URL : http://localhost:8080/Application/Login.jsp. When a user enter this url in IE then, how it get Login.JSP Page exactly? what are the processes will happen from when we submit the URL to get Login.jsp?
how to create multithreaded program? : Java thread
What are the important methods of java exception class?
Does java support function overloading, pointers, structures, unions or linked lists?