Maria, If the left hand operand could be an instance of the right hand operand then the code will compile without error. If the left hand operand could never be an instance of the right hand operand then the code will not compile. The compile-time error is generated when the compiler recognizes that you are asking a question that can never produce a result other than "false". A reference of type Red could never refer to an object instance of type Blue; therefore, the compiler recognizes that you are asking a question that could never produce a result other than false. Rather than allow you to believe that you have asked a valid question a compile-time error is generated to warn you that your code contains an obvious error. In the second program example the left hand operand could be an instance of the right hand operand type so the code compiles and runs without error. Thank you for using my exam.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Joined: Jul 14, 2002
Hi Dan, Im still confused According to Java API, AbstractMap and AbstractCollection are peer classes (they both inherit from Object). AbstractList and AbstractSet are subclasses of AbstractCollection. Therefore, there should be no way that an AbstractMap reference type can be an instanceof Collection. Based on my understanding of what you've said, if there's no way that a reference can be an instance of an array, interface or class, a Compiler error occurs. But why does it not happen in the above scenario ? [ February 03, 2003: Message edited by: Maria Garcia ] [ February 03, 2003: Message edited by: Maria Garcia ]
Since some sub-class of AbstractMap might implement the Collection interface, the compiler has to allow you to test whether a is an instanceof Collection. Unless the type of a reference is a final class, the compiler cannot be sure at compile time that the reference is not an instanceof any interface. // System.out.print((a instanceof AbstractCollection)+","); // compile error System.out.print((a instanceof Runnable)+","); // compiles OK, prints false [ February 03, 2003: Message edited by: John Paverd ]
I believe that the rule of thumb here might be: "As long as one of the operands of the instanceof operator is an reference to an interface as left operand OR an interface as right operand the comparison PASSES the compiler". That holds even though the compiler might have enough information to determine that the comparison is always false. In the code below line 5 is OK although, in this context, p1 could never hold a Red object. Same for lines 8,9. I guess the compiler doesn't want to get too involved ;-)
[ February 04, 2003: Message edited by: Dan Culache ] [ February 04, 2003: Message edited by: Dan Culache ]
Hi all, I completely agree with Maria...So my question goes to Dan and John. Can you explicitly explain the problem posted by Maria regarding the Collection example. I had the same confusion...that if an AbstractMap is no way related to Collection (according to the api...Collection and Map are not interchangeable) then why there is no compile error for the statement AbstractMap a = new HashMap(); System.out.println(a instanceof Collection); If you can clearly explain specifically for this problem I shall be glad. rahul
regards,<p>rahul <p>SCJP 1.4<br />--------<br />Life is great, make it meaningful<br />---------------------------------
Two lines that might help you see the difference -
And a slight variation of the above -
Joined: Jul 02, 2002
Originally posted by Rahul Gupta: Hi all, I completely agree with Maria...So my question goes to Dan and John. Can you explicitly explain the problem posted by Maria regarding the Collection example. rahul
If the right hand operand of the instanceof operator is an interface then Java assumes that some subclass of the left hand operand might implement the interface. As pointed out in the thread suggested by Alfred, the compiler does not do a lot of additional checks to verify that the hypothetical subclass actually exists.