File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instanceof question

 
Maria Garcia
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maria Garcia
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Dan Culache
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Alfred Kemety
Ranch Hand
Posts: 279
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ashish Hareet
Ranch Hand
Posts: 375
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two lines that might help you see the difference -

And a slight variation of the above -

HTH
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic