The Artima Developer Community
Sponsored Link

Legacy Design Forum
Canonical Object Idiom



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.


null instanceof Object == false

Posted by Steven Pearson on August 14, 2000 at 9:10 AM

> 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?

Looks fine to me.

I think this is even simpler though

if( ! (obj instanceof this.getClass() )
return false;

// Safe Cast and compare

The language spec says that instanceof returns false if obj is null.

Section 15.20.2 Type Comparison Operator instanceof

If you're interested =)


Sponsored Links

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