Skip to main content

Brain Teaser #1: the same but not equal

Posted by schaefa on January 15, 2004 at 9:53 PM PST

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