Results 1 to 11 of 11
- 08-21-2010, 03:21 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 4
- Rep Power
- 0
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
yaduLast edited by Yadu; 08-21-2010 at 04:10 PM.
-
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.
- 08-21-2010, 03:34 PM #3
Does the author give a reason for the statement?
String is immutable and its equals() method is THE way to compare Strings.
-
delete: in error
Last edited by Fubarable; 08-21-2010 at 04:22 PM.
- 08-21-2010, 04:15 PM #5
Member
- Join Date
- Aug 2010
- Posts
- 4
- Rep Power
- 0
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
- 08-21-2010, 04:16 PM #6
Member
- Join Date
- Aug 2010
- Posts
- 4
- Rep Power
- 0
I dont understand the message, i didnt post this anywhere...have just a quesion
- 08-21-2010, 04:21 PM #7
Member
- Join Date
- Aug 2010
- Posts
- 4
- Rep Power
- 0
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"
-
- 08-22-2010, 09:12 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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
- 08-22-2010, 04:24 PM #10
Sounds like collections should have change listeners. Or the user should call the collection with a validate like method.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
- 08-22-2010, 04:43 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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
-
equals method :::::HELP:::::
By alihht in forum New To JavaReplies: 5Last Post: 03-09-2010, 07:19 AM -
ArrayLists compareTo method, equals method
By random0munky in forum New To JavaReplies: 2Last Post: 10-26-2009, 07:20 PM -
equals method
By mani_miit in forum Advanced JavaReplies: 7Last Post: 09-09-2009, 10:26 PM -
[SOLVED] [newbie] Is my method mutable?
By jon80 in forum New To JavaReplies: 4Last Post: 05-20-2009, 11:29 PM -
equals method
By timkd127 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks