wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instaceof operator 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 "instaceof operator" Watch "instaceof operator" New topic
Author

instaceof operator

Sar Gops
Greenhorn

Joined: Dec 26, 2007
Posts: 2
Hi friends,

In K & B ,page no:286 i foung Foo[1] type is compared against Object,Foo,faces,Bar.and the result is true.can any one tell me what is the reasons.also Please tell me about the last three rows in that table.

Thanks in advance
Thirugnanam Saravanan
Ranch Hand

Joined: Dec 13, 2007
Posts: 81
For this question, the scenarion given in the book is :

interface Face { }
class Bar implements Face{ }
class Foo extends Bar{ }

The Thumb rule for working with instanceof Operator is as follows :

whenever a Class extends another Class or implements an interface, the following condition will return true :

1. sub-class instanceof super-Class - TRUE
2. implementing-class instanceof interface - TRUE

The vice versa is false in both 1 and 2 above.

Ok .. coming back to your question..

Foo[1] represents a Foo object which extends Bar (means it implicitly implements the interface Face also).

So Foo instanceof Foo,Bar,Face,Object will return TRUE.

Also Foo[] is an array. Any Array is an instanceof Object.

So Foo[] instanceof Object will return TRUE.


Saravanan
SCJP 5.0(98%), SCWCD 5.0 (100%), OCA
Jart Bo
Ranch Hand

Joined: Oct 22, 2007
Posts: 144
In other words, the instanceOf operator checks if the object passes the IS-A test.


SCJP, SCWCD
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: instaceof operator
 
Similar Threads
problem using jstl
Explain this question........
instanceof
Can I have some foo please?
Chap.10 Development , Q5 pg813 : not understood answer and cannot run code