• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Mike Kelly
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic