wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 38100
    
  22
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: 38100
    
  22
Yesterday, I wrote: . . . Is that a quote from Joshua Bloch’s book? . . .
It is on page 35 in the 2nd edition of Effective Java.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Object equals
 
Similar Threads
Do not use primitive types for method arguments?
how to ignore String case when checking through Set keys?
Which is best to use in hashcode?
Needs answers of some important questions
overriding equals