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 Using instanceof operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Using instanceof operator" Watch "Using instanceof operator" New topic

Using instanceof operator

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
interface Face { }

class Bar implements Face { }

class Foo extends Bar { }

Results when using the instanceof operator :

Foo [] instanceof (Foo , Bar , Face ) --- False

Foo[1] instanceof (Foo, Bar , Face , Object ) --- True

Can some one please explain the above ?
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774

It is because, any Foo object is an instance of Foo.
Foo is an instance of Bar, because Foo extends Bar.
Foo is an instance of Face because Class Bar implements Face and Bar is a direct super-class of Foo, so Foo is an instance of Face as well.
Any class in Java is an instance of Object.

Hope this helps,

My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
John Mercier

Joined: Nov 23, 2006
Posts: 9

Also, Foo[] is an Array not a Foo.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774

Additionally, when you ask some question, please separate it from comments. Else hard time to understand your question.

Please check this out

Foo[] is an array and an array is only an instance of Object.
Foo[1] is talking about a specific object.

Hope this will help you clear your doubt.

Happy Preparation,
Anup Om
Ranch Hand

Joined: Dec 30, 2009
Posts: 62
Foo [] instanceof (Foo , Bar , Face ) --- False

This will cause compilation error(if written as Java code) as Foo or Bar or Face isn't supertype of Foo[]. (i.e they don't fall under same hierarchy).

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
Thanks all for responding.
sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

It is because we have made Foo[] as an array object

Thanks & Regards
Sumit Kothalikar
It is sorta covered in the JavaRanch Style Guide.
subject: Using instanceof operator
Similar Threads
Doubt: K&B 1.5 p. 286; table 4-1 (´┐Ż instanceof´┐Ż)
instanceof doubt
instanceof ?
instanceof operand
K&B book page 286 table 4-1: Is the array instnaceof result correct?