File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instaceof operator" Watch "instaceof operator" New topic

instaceof operator

Sar Gops

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.

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.

I agree. Here's the link:
subject: instaceof operator
It's not a secret anymore!