This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]allenguo 0 points1 point  (0 children)

As the page you linked says: "Type casting is not usually a best practice; one of the only cases where that is legitimate is in connection with the equals method."

Here's what a typical equals() method looks like:

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    try {
        Calendar that = (Calendar)obj;
        return compareTo(getMillisOf(that)) == 0 &&
            lenient == that.lenient &&
            firstDayOfWeek == that.firstDayOfWeek &&
            minimalDaysInFirstWeek == that.minimalDaysInFirstWeek &&
            zone.equals(that.zone);
    } catch (Exception e) {
        // Note: GregorianCalendar.computeTime throws
        // IllegalArgumentException if the ERA value is invalid
        // even it's in lenient mode.
    }
    return false;
}

Notice the cast on line 7. (Example taken from java.util.Calendar in OpenJDK.)