Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 86975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:19:49+00:00 2026-05-10T22:19:49+00:00

http://leepoint.net/notes-java/data/expressions/22compareobjects.html It turns out that defining equals() isn’t trivial; in fact it’s moderately hard

  • 0

http://leepoint.net/notes-java/data/expressions/22compareobjects.html

It turns out that defining equals() isn’t trivial; in fact it’s moderately hard to get it right, especially in the case of subclasses. The best treatment of the issues is in Horstmann’s Core Java Vol 1.

If equals() must always be overridden, then what is a good approach for not being cornered into having to do object comparison? What are some good ‘design’ alternatives?

EDIT:

I’m not sure this is coming across the way that I had intended. Maybe the question should be more along the lines of ‘Why would you want to compare two objects?’ Based upon your answer to that question, is there an alternative solution to comparison? I don’t mean, a different implementation of equals. I mean, not using equality at all. I think the key point is to start with that question, why would you want to compare two objects.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:19:50+00:00Added an answer on May 10, 2026 at 10:19 pm

    If equals() must always be overridden, then what is a good approach for not being cornered into having to do object comparison?

    You are mistaken. You should override equals as seldom as possible.


    All this info comes from Effective Java, Second Edition (Josh Bloch). The first edition chapter on this is still available as a free download.

    From Effective Java:

    The easiest way to avoid problems is not to override the equals method, in which case each instance of the class is equal only to itself.

    The problem with arbitrarily overriding equals/hashCode is inheritance. Some equals implementations advocate testing it like this:

    if (this.getClass() != other.getClass()) {     return false; //inequal } 

    In fact, the Eclipse (3.4) Java editor does just this when you generate the method using the source tools. According to Bloch, this is a mistake as it violates the Liskov substitution principle.

    From Effective Java:

    There is no way to extend an instantiable class and add a value component while preserving the equals contract.

    Two ways to minimize equality problems are described in the Classes and Interfaces chapter:

    1. Favour composition over inheritance
    2. Design and document for inheritance or else prohibit it

    As far as I can see, the only alternative is to test equality in a form external to the class, and how that would be performed would depend on the design of the type and the context you were trying to use it in.

    For example, you might define an interface that documents how it was to be compared. In the code below, Service instances might be replaced at runtime with a newer version of the same class – in which case, having different ClassLoaders, equals comparisons would always return false, so overriding equals/hashCode would be redundant.

    public class Services {      private static Map<String, Service> SERVICES = new HashMap<String, Service>();      static interface Service {         /** Services with the same name are considered equivalent */         public String getName();     }      public static synchronized void installService(Service service) {         SERVICES.put(service.getName(), service);     }      public static synchronized Service lookup(String name) {         return SERVICES.get(name);     } } 

    ‘Why would you want to compare two objects?’

    The obvious example is to test if two Strings are the same (or two Files, or URIs). For example, what if you wanted to build up a set of files to parse. By definition, the set contains only unique elements. Java’s Set type relies on the equals/hashCode methods to enforce uniqueness of its elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 88k
  • Answers 88k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Is a know issue when using .NET rewriting. You need… May 11, 2026 at 5:46 pm
  • Editorial Team
    Editorial Team added an answer You can create a hook on the receiving side. Add… May 11, 2026 at 5:46 pm
  • Editorial Team
    Editorial Team added an answer Consider the Page's PreRender event. That way binding has happened… May 11, 2026 at 5:46 pm

Related Questions

http://leepoint.net/notes-java/data/expressions/22compareobjects.html It turns out that defining equals() isn't trivial; in fact it's moderately hard
http://en.wikipedia.org/wiki/ICalendar I'm working to implement an export feature for events. The link above lists
http://en.wikipedia.org/wiki/Diamond_problem I know what it means, but what steps can I take to avoid
http://thedailywtf.com/Articles/The-Hot-Room.aspx You see how at the bottom there're links to the next and previous
http://developer.yahoo.com/javascript/howto-proxy.html Are there disadvantages to this technique? The advantage is obvious, that you can

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.