Results 1 to 11 of 11
  1. #1
    Yadu is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default Mutable Objects having equals method

    Program Development in Java by Barabara Liskov mentions that "mutable Objects in Java shouldn't implement equals method" Chapter 5, sidebar 5.1. But many of Java's Collection like Vector which are mutable and implement equals method for comparing state (elements) rather than relying on reference equals. Can someone shed light on the anomaly

    yadu
    Last edited by Yadu; 08-21-2010 at 05:10 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm not sure I see the paradox here. The text is saying that [b]im[b]mutable objects shouldn't implement equals and you're showing an example of a mutable class implementing equals which all makes sense.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Does the author give a reason for the statement?
    String is immutable and its equals() method is THE way to compare Strings.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  5. #5
    Yadu is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default

    The subject was right, but text had typo..mutable shouldnt implement equals, only immutables should implement equals....STring is immutable and implements equals that is correct, Vector is mutable and ishould rely of reference equals mplments equals...that is the paradox

  6. #6
    Yadu is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default

    I dont understand the message, i didnt post this anywhere...have just a quesion

  7. #7
    Yadu is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default

    I think the answer lies in this Java Specification
    "For mutable objects, the answer is not always so clear. Should equals() and hashCode() be based on the object's identity (like the default implementation) or the object's state (like Integer and String)? There's no easy answer -- it depends on the intended use of the class. For containers like List and Map, one could have made a reasonable argument either way. Most classes in the Java class library, including container classes, err on the side of providing an equals() and hashCode() implementation based on the object state"

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Yadu View Post
    I dont understand the message, i didnt post this anywhere...have just a quesion
    You are correct and I don't know what I was thinking. I'm terribly sorry for the mix up.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Suppose you stick a mutable object in a collection that relies on the equals( ... ) method and/or the hashCode() method of the object. The object is mutable so you can change its state behind the back of the collection and so you mess up the bookkeeping of the collection in a most horrible way.

    OTOH not implementing those methods for your mutable object reduces the flexibility of it all so for aesthetic reasons you shouldn't implement them but for practical reasons you should do so ...

    kind regards,

    Jos

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    The object is mutable so you can change its state behind the back of the collection and so you mess up the bookkeeping of the collection
    Sounds like collections should have change listeners. Or the user should call the collection with a validate like method.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Norm View Post
    Sounds like collections should have change listeners. Or the user should call the collection with a validate like method.
    That'd involve more code: each mutable object should be a source of events (and all the add and remove listeners stuff) and the collections should register themselves whenever an object is added to them. IMHO knowing what you're doing can be just as effective ;-) imho protecting code to just stupidity is the next generation of 'defensive programming' and that habit has just bloated a lot of code ...

    kind regards,

    Jos

Similar Threads

  1. equals method :::::HELP:::::
    By alihht in forum New To Java
    Replies: 5
    Last Post: 03-09-2010, 08:19 AM
  2. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 08:20 PM
  3. equals method
    By mani_miit in forum Advanced Java
    Replies: 7
    Last Post: 09-09-2009, 11:26 PM
  4. [SOLVED] [newbie] Is my method mutable?
    By jon80 in forum New To Java
    Replies: 4
    Last Post: 05-21-2009, 12:29 AM
  5. equals method
    By timkd127 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 07:52 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •