GeeCON Prague 2014*
The moose likes Mock Exam Errata and the fly likes jwhiz exam #1 Q4, equals( )  Need a Ranch hand Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "jwhiz exam #1 Q4, equals( )  Need a Ranch hand" Watch "jwhiz exam #1 Q4, equals( )  Need a Ranch hand" New topic
Author

jwhiz exam #1 Q4, equals( ) Need a Ranch hand

Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
What will happen if you compile/run the following code?
Class MyClass
{
int x;
MyClass(int i)
{
x = i;
}
public static void main(string arg[])
{
MyClass m1 = new MyClass(100);
MyClass m2 = new MyClass(100);
if (m1.equals(m2))
{
System.out.println("Both are equal");
}
else
{
System.out.println("Both are unequal");
}
}
}
Answer given is "unequal". Roberts and Heller Certification study guide says that "for x.equals(y) the test y instanceof x must be true. If this is not the case, then equals() must return false.
Seems like a contradiction with this mock exam, is it?
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Mike
In the Object class the == method checks to see if two references refer to the same object in memory (if both point to the same memory location). The equals method in Object simply does an == comparison. In order to get any other functionality from equals (like a deep comparison) you need to override it in your class, which isn't done in the code you posted. So you get the Object equals method and because the two variables don't contain the same object you get unequal.
The String class is one class that does override equals so it compares the actual contents of the strings refered to by each variable.
What the book is refering to is that the first thing == does is check to see if the two objects that are being compared are of the same class.
hope that helps
Dave


Dave
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Mike,
What Dave said is correct. If you check the API for the Object.equals() method you'll see:

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any 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).

Are you sure RHE said 'instanceOf' and not '=='? The 'instanceOf' operator checks the 'type' of the reference variables; the comparison operator '==' checks to see if the reference variables point to the same object in memory. The two operators are not interchangeable.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited July 26, 2001).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
Originally posted by Dave Vick:
Mike
In the Object class the == method checks to see if two references refer to the same object in memory (if both point to the same memory location). The equals method in Object simply does an == comparison. In order to get any other functionality from equals (like a deep comparison) you need to override it in your class, which isn't done in the code you posted. So you get the Object equals method and because the two variables don't contain the same object you get unequal.
The String class is one class that does override equals so it compares the actual contents of the strings refered to by each variable.
What the book is refering to is that the first thing == does is check to see if the two objects that are being compared are of the same class.
hope that helps
Dave

Thanks Dave & Jane. I understand now. Because I knew it worked with strings, I thought it worked everywhere. I read the method in the API. You confirm the need to override the equals(). Page 53 of Roberts, Heller & Ernest Certification Guide does refer to the instanceof test.
Thanks very much
Mike
 
GeeCON Prague 2014
 
subject: jwhiz exam #1 Q4, equals( ) Need a Ranch hand