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?
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
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.
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".
Joined: Dec 16, 2000
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().
Joined: Dec 06, 2000
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().