I was under assumption that
String is just another object type and instanceof can be used to
test if a particular object is of a particular type or descended from that type.
When I try to test if an object is of type String with instanceof operator(haven't found need for this, but still think concept should work) receive compile error as below. In this case t is instance of basic class Ticker which extends component. Source code shown below.
I was expecting this to compile with first two tests to be true and last test to be false, but do not get that far.
>>>
C:\TestTicker2.java:20: inconvertible types
found : Ticker
required: java.lang.String
if (t instanceof String)
^
1 error
<<<
Ticker.java
>>>
import java.awt.*;
class Ticker extends Component
{
int tick;
}
<<<
TestTicker2
>>>
import java.awt.*;
public class TestTicker2
{
public static void main(String args[])
{
Ticker t = new Ticker();
if (t instanceof Ticker)
{
System.out.println("Is a Ticker");
}
if (t instanceof Component)
{
System.out.println("Is a Component");
}
if (t instanceof String)
{
System.out.println("Is a String");
}
}
}
<<<