File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes equals() 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 » Java » Java in General
Bookmark "equals()" Watch "equals()" New topic


Bob Tank

Joined: Oct 05, 2000
Posts: 9
Which of these statements are true?
A. If the references x and y denote two different objects, then the experssion x.equals(y) is always false.
B. All array objects have a method named clone.
Answer is B only. Why isn't answer A. true???
Noah Carroll
Ranch Hand

Joined: Sep 20, 2000
Posts: 96
x.equals(y); could be false because the equals() method tests the value of the object not the reference.
however, i don't think it would always be false, because if both objects have the same values then it would be true.
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
For instance:
String String1 = "abc";
String String2 = "abc";
String1.equals(String2) returns true.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
While your answer is technically correct, I feel it is not the right example in this case because String1==String2will return true also.
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
The equals method is defined in the Object class and it does nothing but basic reference comparison. Several subclasses of Object override the equals method to do content based comparison. The examples are the wrapper classes and the String class.
However, there are some derivatives of Object that doesnot override the equals method. One such class is the StringBuffer. If a class (derived from Object ) does not override the equalsmethod, then as per the OO rules, calling equals method on an instance of such a class will call the Object.equals() method.... and you know what happens
Hence the answer A is false!
Hope that helps,
[This message has been edited by Ajith Kallambella (edited October 10, 2000).]

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Subramaniam Chidambaram

Joined: Oct 18, 2000
Posts: 7
If i understand correctly.
If the equals() method is not overridden then the objects
equals() method is called which is same as ==


Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
Yes, Subbu, that is true.
I agree. Here's the link:
subject: equals()
It's not a secret anymore!