When writing one’s own classes, is it always necessary to override equals(Object o)?
If I don’t, will it automatically check that all the fields are the same? Or does it just check if the two variables point to the same object?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If one is writing a class that is going to have its objects be compared in some way, then one should override the
equalsandhashCodemethods.Not providing an explicit
equalsmethod will result in inheriting the behavior of theequalsmethod from the superclass, and in the case of the superclass being theObjectclass, then it will be the behavior setforth in the Java API Specification for theObjectclass.The general contract for providing an
equalsmethod can be found in the documentation for theObjectclass, specifically, the documentation of theequalsandhashCodemethods.