Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 158
Invalid instanceof
ОглавлениеOne area the exam might try to trip you up on is using instanceof
with incompatible types. For example, Number
cannot possibly hold a String
value, so the following causes a compilation error:
public void openZoo(Number time) { if(time instanceof String) // DOES NOT COMPILE System.out.print(time); }
If the compiler can determine that a variable cannot possibly be cast to a specific class, it reports an error.