posted 13 years ago
Using instanceof will give a compiler error any time there's absolutely no way the check can be true (because it must be an error - why would you check in that case?).
Now, looking at two of the cases you've highlighted, at first glance it would seem they can't be true. But the point here is that Map is an interface, so it is possible to have an object where the checks succeed. Consider these classes:
The same trick doesn't work with String because, as Harsha says, it's final, so you can't subclass it.
If you change the type of m to HashMap, you'll find the Date one fails as well. You can have an object that's a Date and a Map (see above), but you can't have an object that's a Date and a HashMap, as that would need multiple inheritance.