wood burning stoves*
The moose likes Beginning Java and the fly likes Object equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object equals" Watch "Object equals" New topic
Author

Object equals

Kasun Wixkramanayake
Ranch Hand

Joined: Feb 09, 2010
Posts: 74

CaseInsensitiveString cis = new CaseInsensitiveString("Polish");
String s = "polish";

cis.equals(s)//true
s.equals(cis)//flase

why it happens like that
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
You appear to have posted in the wrong location; let’s move your discussion somewhere more appropriate.

Is that a quote from Joshua Bloch’s book? Doesn’t he explain it? Which bit do you not understand?
Kasun Wixkramanayake
Ranch Hand

Joined: Feb 09, 2010
Posts: 74
I couldnt understand what is meant by symmetry and One-way interoperability
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kasun Wixkramanayake wrote:I couldnt understand what is meant by symmetry and One-way interoperability


Symmetry means that if X and Y are both non-null, then X.equals(Y) must return the same results as Y.equals(X). The CaseInsensitiveString class violates that.

It looks like "one-way interoperability" basically refers to the specific manner in which it is violating symmetry. By using that instanceof test, CaseInsensitiveString.equals(String) can be true, but String.equals(CaseInsensitiveString) can never be true.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Yesterday, I wrote: . . . Is that a quote from Joshua Bloch’s book? . . .
It is on page 35 in the 2nd edition of Effective Java.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object equals