The Artima Developer Community
Sponsored Link

Legacy Design Forum
Canonical Object Idiom

Advertisement

Advertisement

This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Simpler code for equals(); please review

Posted by Vijay Varanasi on December 14, 1999 at 1:40 AM

Hi!

The code for equals() in the article ensures that the Object passed as an argument is of the same type as itself by trying a typecast; failure results in a ClassCastException which is caught:

public boolean equals(Object o) {

if (o == null) {
return false;
}

Worker w;
try {
w = (Worker) o;
}
catch (ClassCastException e) {
return false;
}
.
.
.


I saw this simpler code elsewhere:

public boolean equals(Object obj)
{
if (obj != null && (obj instanceof this.getClass()))
{
.
.
.

Is this OK? Or are there any hidden glitches in this that aren't apparent to me?

Thanks,
Vijay




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us