-3

I know that == operator is applicable for content comparison for primitive types and reference comparison for objects.

Similarly, .equals() method of object class is for reference comparison of objects and content comparison in strings and wrapper classes.

But the following points which I took from SCJP/OCJP notes, I am not able to understand. I looked on the web and find only the differences between == and .equals but not the relation as how it is mentioned below.

  • If r1==r2 is true then r1.equals(r2) is always true.
  • If r1==r2 is false then r1.equals(r2) may return true (or) false.
  • If r1.equals(r2) is true then r1==r2 may return true (or) false.
  • If r1.equals(r2) is false then r1==r2 is always false.
kittu
  • 6,662
  • 21
  • 91
  • 185
  • 4
    What is your question? – Oliver Charlesworth Apr 04 '15 at 09:32
  • 1
    The statement "`equals()` method of object class is for reference comparison of objects and content comparison in strings and wrapper classes" is wrong. There are many classes where `equals()` is defined as a comparison of content. Its purpose is to declare a "value" equality where that makes sense (Strings, numbers, colors, collections, coordinates), and it defaults to object identity where there is no "value" equality as far as the author was concerned. – RealSkeptic Apr 04 '15 at 09:57
  • @RealSkeptic The statement "equals() method of object class is for reference comparison of objects and content comparison in strings and wrapper classes" is CORRECT. There might be other classes where equals() is defined but as to what OP has mentioned is right –  Apr 04 '15 at 10:16
  • @KKK - Only if you consider things such as `Point` or `List` not to be objects... – RealSkeptic Apr 04 '15 at 11:39

1 Answers1

1

These requirements ensure that the equals method will have the semantics that people expect for the concept of equivalence, and the logical/mathematical properties.

So, we expect equivalence to be transitive: if A equals B and B equals C, then A equals C. And we expect symmetry: if A equals B then B equals A. And we expect equality to be reflexive: A equals A.

Raedwald
  • 46,613
  • 43
  • 151
  • 237