Tuesday, April 28, 2009

Trick and Tips with instanceOf operator

The tricky bit about instanceof operator can be that if object on the left side is null, the condition evaluates to false. Therefore there is no need for null check after a class cast as in the following example.

Do not use instanceOf with this.

Do not use chained instanceOf operators use polymorphism instated of that.
From Effective C++, by Scott Meyers :
"Anytime you find yourself writing code of the form "if the object is of type T1, then do something, but if it's of type T2, then do something else," slap yourself.

http://www.javapractices.com/topic/TopicAction.do?Id=31
http://hanuska.blogspot.com/2006/08/tricky-instanceof-operator.html

No comments: