Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Khalid question on .equals()

 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from Khalid's review questions:
Which of these statements are true?
Select all valid answers.
(a) If the references x and y denote two different objects, then the expression x.equals(y) is always false.
(b) If the references x and y denote two different objects, then the expression (x.hashCode()==y.hashCode()) is always false.
(c)The hashCode() method in the Object class is declared final.
(d)The equals()method in the Object class is delared final.
(e)All array objects have a method named clone.
The answer given is (e) But isn't answer (a) true as well?
 
Sandeep Nachane
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean,
Following is my take on the option (a).
You would have been right if ALWAYS was not part of the option (a) because String object overrides equals() and provide the notion of true equality. i.e It overrides equals() to return true if the two String objects have same contents even though you create these as two different objects.
Just my .02 cents
Sandeep Nachane

------------------
Visit my homepage at
www.tipsmart.com
 
S Dave
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Option (a) is not true at all because if 2 different objects are declared as:
Integer x = new Integer(9);
Integer y = new Integer(9);
if(x.equals(y))
System.out.println("equal ");
else
System.out.println("not equal ");
the output would be - equal.
 
Rosie Vogel
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sweekriti is right, equals() looks at the CONTENT of an object.
But also at type compatibility! Hence, if you declared variable y in the previous post as, for example, a Float instead of an Integer, your output would be "not equal".
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand how equals() works. The point of my question was because in the object class it looks at the reference and not the contents, and the question asked about objects and nothing else, so I wasn't thinking further down the hierarchy in classes that override the equals().
 
Sandeep Nachane
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question asks about the "object" (i.e all objects in general)and not Object class, hence you need to think further down the hierarchy in classes that override the equals().

-Sandeep Nachane www.tipsmart.com


[This message has been edited by Sandeep Nachane (edited February 14, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic