File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Khalid question on .equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Khalid question on .equals()" Watch "Khalid question on .equals()" New topic
Author

Khalid question on .equals()

Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
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

Joined: Dec 06, 2000
Posts: 57
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


<A HREF="http://www.tipsmart.com" TARGET=_blank rel="nofollow">www.tipsmart.com</A>
S Dave
Ranch Hand

Joined: Jan 28, 2001
Posts: 103
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

Joined: Jan 31, 2001
Posts: 228
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

Joined: Dec 16, 2000
Posts: 625
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

Joined: Dec 06, 2000
Posts: 57
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).]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Khalid question on .equals()
 
Similar Threads
Question 10.2 of khalid mughal's book
x.hasCode() == y.hashCode()
About Object
equals()
== and equal