Implementing equals for user-dened types
Seems easy, but requires some care.
typically unsafe to use equals() with inheritance (would violate symmetry)
public final class Date implements Comparable<Date> { private final int month; private final int day; private final int year; ... public boolean equals(Object y) { if (y == this) return true; if (y == null) return false; if ([Link]() != [Link]()) return false; Date that = (Date) y; if ([Link] != [Link] ) return false; if ([Link] != [Link]) return false; if ([Link] != [Link] ) return false; return true; } }
must be Object. Why? Experts still debate.
optimize for true object equality check for null objects must be in the same class (religion: getClass() vs. instanceof)
cast is guaranteed to succeed check that all significant fields are the same
10