-3

Below is written in javadocs

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Does it mean object1.equals(object2) return true only when object1==object2.

Below example at In Java, what is a shallow copy?

class Foo { private Bar myBar; ... 
public Foo shallowCopy() { 
 Foo newFoo = new Foo(); newFoo.myBar = myBar; return newFoo; }  
public Foo deepCopy() { Foo newFoo = new Foo(); newFoo.myBar = myBar.clone(); //or new Bar(myBar) or myBar.deepCopy or ... return newFoo; } } Foo myFoo = new Foo();
Foo sFoo = myFoo.shallowCopy();
Foo dFoo = myFoo.deepCopy(); 

myFoo.myBar == sFoo.myBar => true
myFoo.myBar.equals(sFoo.myBar) => true
myFoo.myBar == dFoo.myBar => false
myFoo.myBar.equals(dFoo.myBar) => true 

If First understading is correct how come myFoo.myBar.equals(dFoo.myBar) => true

Community
  • 1
  • 1
M Sach
  • 33,416
  • 76
  • 221
  • 314
  • 3
    If you gave asked 35 questions you should know how to format your code ;) – Peter Lawrey Jul 14 '11 at 20:35
  • 2
    You've asked 34 previous questions. With respect, you *really should* be formatting things correctly by now. Please edit your question and format the block quotes, so we don't have to **guess** where they start and end, and format the code so it's readable. There was a handy **How to Format** box to the right when you were asking your question and a preview area underneath it. 34 questions in, formatting and checking the result should be automatic. *Edit*: @Amir has been kind enough to do it for you, but really he shouldn't have had to, that's something people do for newbies. – T.J. Crowder Jul 14 '11 at 20:35
  • Srry for inconvenience. I wiil take care of it in future – M Sach Jul 15 '11 at 10:58

2 Answers2

7

The default implementation for equals is the same as ==

However it is common practice to override this method and give it an implementation where two objects are equal if the type and contents are the same. It should always be true when == is true.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thanks. Actuaaly i am interested about below specific line in code myFoo.myBar.equals(dFoo.myBar) =>true As per my understanding it should be false as its a deep copy anf we have not overriden equals method – M Sach Jul 15 '11 at 10:59
  • That is what I am talking about. The most logical explaination if that `Bar` has an equals method which accepts objects which contain the same values. – Peter Lawrey Jul 15 '11 at 11:02
0

No, o1.equals(o2) should return true when o1 == o2 is true, but it can also return true under other circumstances.

o1 == o2 returns true if o1 and o2 reference the same object, i.e. the same piece of memory.

However we're often interested in whether two objects are the same based on their properties other than their location in memory. For example if I execute new String("Hello World") twice, then the two strings are the same based on the sequence of characters they represent, but they are not the same object in memory.

The JavaDocs are talking about the implementation of equals() provided by the Object class itself, which returns true when the == operator returns true. When you create a subclass of object, you can re-implement the equals method to provide the kind of equality described above.

brabster
  • 42,504
  • 27
  • 146
  • 186