learn java
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof Problem" Watch "instanceof Problem" New topic

instanceof Problem

Andreas Sandberg
Ranch Hand

Joined: Sep 14, 2005
Posts: 31

This question comes out of the K&B book, self test question #2 on Pg.195.
The question is as follows:

2. Given the following.

1. import java.awt.*;
2. class Ticker extends Component {
3. public static void main(String[] args) {
4. Ticker t = new ticker();
6. }
7. }

which two of the following statements, inserted independnently, could legally be inserted into line 5 of this code?(choose two)
A. boolean test = (Component instanceof t);
B. boolean test = (t instanceof Ticker);
C. boolean test = t.instanceof (Ticker);
D. boolean test = (t instanceof Component);
E. boolean test = t.instanceof (Object);
F. boolean test = (t instanceof String);

The answer is B and D, but why not F? I get a compile time error when inserting the expression. String is a subclass of Object so why can't I use the instanceof operator? Although I know they are entirely different objects, isn't that the whole point of instanceof. It should tell me if they are the same type of object so then I can make an implicit cast. Like when you overide the equals method. Any clarrification is appreciated.


[ October 06, 2005: Message edited by: Andreas Sandberg ]

"When the compilers not happy ain't nobody happy"
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340

instanceof operator flags a compile time error if the object is not at all related to the class its being compared in any manner. Here in your example t which is an object of Ticker class is ofcourse a subclass of Component class, but then there is no inheritance relation between Ticker and a String.

Same is the case with String and StringBuffer.

String s;
boolean b=s instanceof StringBuffer; //compile time error

it says
main.java:10: inconvertible types
found : java.lang.String
required: java.lang.StringBuffer
boolean b=s instanceof StringBuffer;
1 error

Hope this helps

Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 980

Have a look at this...


[ October 06, 2005: Message edited by: A Kumar ]
Andreas Sandberg
Ranch Hand

Joined: Sep 14, 2005
Posts: 31
Muchos gracias. Still think it would make sense to compare two reference types with the instanceof operator and have it return false rather than a compile time exception though.

I agree. Here's the link: http://aspose.com/file-tools
subject: instanceof Problem