The Source for Java Technology Collaboration
User: Password:



Andreas Schaefer's Blog

January 2004 Archives


Brain Teaser #1: the same but not equal

Posted by schaefa on January 15, 2004 at 09:53 PM | Permalink | Comments (21)

A colleque of mine just came up with the results of a test that threw me off and it took us quite some time to figure it out. So how good, do you think, are your Java skills?

The class 'ABC' does not overwrite equals() method and derives directly from java.lang.Objects and everything is pure Java.
ABC a = ;
System.out.println("Is of class: " + a.getClass().getName()); // Yields to 'ABC@
System.out.println("Is the same: " + ( a == a )); // Yields to true, of course
System.out.println("Is equal: " + a.equals(a)); // Yields to false ???
You read right the object 'a' is the same but not equal. Does anyone knows why?
Hint: accept the result of equals().

Have fun - Andy



Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds