File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof 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 "instanceof " Watch "instanceof " New topic
Author

instanceof

reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
I got a question:

Which of the following statements are true?

1) The instanceof operator can be used to determine if a reference is an instance of a class, but not an interface.
2) The instanceof operator can be used to determine if a reference is an instance of a particular primitive wrapper class
3) The instanceof operator will only determine if a reference is an instance of a class immediately above in the hierarchy but no further up the inheritance chain
4) The instanceof operator can be used to determine if one reference is of the same class as another reference thus

I chose 2 and 4, but the correct answer is 2, in the answer, it says 'The instanceof operator can only be used to make a static comparison with a class type', I'm not quite understand what it means.

Can someone explain what 'The instanceof operator can only be used to make a static comparison with a class type' is ?

Thank you.


SCJA, SCJP5.0, SCBCD, SCWCD
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

4) The instanceof operator can be used to determine if one reference is of the same class as another reference


This is false because... well, how would you do it? Say you want to write a method to determine if two objects are the same class. How would you do it? If you don't know if an object is a String, or StringBuffer, or Thread object, you can't determine what class it is. (With only the instanceof operator that is)

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

The instanceof operator can only be used to make a static comparison with a class type


It is awkwardly worded. I believe the "static" is not the type of the variable, it is referring to the type checking, as is not dynamic.

For example, you can do this:

a instanceof String
a instanceof Thread

But you can't do this:

a instanceof b.getClass()

Henry
[ June 17, 2005: Message edited by: Henry Wong ]
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
In other words, why not support this:

if (foo instanceof bar) ...

You could not use the class type on the right since it must work with interfaces. You could assume the declared type of bar. Now you've tied conditional logic to a variable type name that may quietly break in many different ways at runtime. Looks like "object instanceof type" is perfect.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof