Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
I'm relatively new to java and am studying for the SCJP. I haven't seen the instanceof operator in use in any code that I've looked at so far, but what's the point?
If you can only use it to test objects against class types that are in the same class hierarchy, won't it always return true if the variable on the left refers to an object that is in the class hierarchy (or false if null) and always not compile if the object refered to is not? Of course, it will return false if the variable is null, but in that case why not just test for null?
It's about polymorphism and storing an object that belongs to a subclass (B extends A) in a ref. var. that is defined as the superclass of B (A). And once you've stored it in a superclass ref. var., you can use instanceof to figure out which subclass the ref.var. object actually is.
Did I get it?
Thanx, DMad [ March 08, 2005: Message edited by: David Madouros ]
If the righthand side is java.lang.Object you'll always get true. After all, everything derives from java.lang.Object! Of course if you're comparing Objects across classloaders this is no longer the case but that's a somewhat obscure scenario that many people will never encounter.