This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof question" Watch "instanceof question" New topic
Author

instanceof question

Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86
Dan Mock #13

The above code gives a compiler error.


The above code returns false,true,true .
How do we know if it comes up with a compiler error or if instanceof returns false ?


SCJP 1.4
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Since an object of type Blue can NEVER be held by a pointer of type Red asking if a Red pointer contains a Blue object is meaningless and will cause a compile error.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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>
Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86
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 ]
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
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 ]

SCJP 1.4
Dan Culache
Ranch Hand

Joined: Jan 24, 2003
Posts: 70
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 ]
Rahul Gupta
Ranch Hand

Joined: Nov 20, 2002
Posts: 37
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 />---------------------------------
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Ok, Here's a thread that I think addressed this subject very well... I hope it will be helpful
http://www.coderanch.com/t/239587/java-programmer-SCJP/certification/Aren-Collection-AbstractMap-peers-common


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Two lines that might help you see the difference -

And a slight variation of the above -

HTH
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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.
 
 
subject: instanceof question
 
Similar Threads
instanceof operator
instanceof
instanceof operator
Doubt from Dan's test
clear my doubt????