my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals() method" Watch "equals() method" New topic

equals() method


Joined: Jun 07, 2001
Posts: 3
Hi, I found this question in the book of A Programmer's Guide to Java Certification by Khalid A.Mughal.
If the references x and y denote two different objects, then the expression x.equals(y) is always false.
True of false? I think it is true, but the answer of the book is false. Can anybody explain this?
I appreciate it.
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

The answer is false because the result of x.equals(y) depends on how x implements the equals() method. With only the information that x and y are different objects, you still cannot say for sure that x.equals(y) is always false.
Peter Haggar has a very good discussion about implementing the equals() method in his book "Practical Java".

[This message has been edited by JUNILU LACAR (edited June 11, 2001).]

Junilu - [How to Ask Questions] [How to Answer Questions]
Marcellus Tryk
Ranch Hand

Joined: May 31, 2001
Posts: 64
The equals() method is meant to test the equivalence of two objects. What equivalence means is up to the implementation of the class. You define equivalence for your class by overriding the equals(Object obj) method inherited from Object.
This is illustrated in the following example. a and b are two different Integer objects but they are considered 'equal'.


[This message has been edited by Tod Tryk (edited June 11, 2001).]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
How about this?

[This message has been edited by Thomas Paul (edited June 12, 2001).]

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Marcellus Tryk
Ranch Hand

Joined: May 31, 2001
Posts: 64
That's interesting - but should work.
- T
I agree. Here's the link:
subject: equals() method
It's not a secret anymore!