Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object equals

 
Kasun Wixkramanayake
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

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

why it happens like that
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldnt understand what is meant by symmetry and One-way interoperability
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yesterday, I wrote: . . . Is that a quote from Joshua Bloch’s book? . . .
It is on page 35 in the 2nd edition of Effective Java.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic