[4083] in java-interest
Re: instanceof operator playing with my mind
daemon@ATHENA.MIT.EDU (Neil Bartlett)
Sun Dec 10 02:25:22 1995
Date: Sun, 10 Dec 1995 00:48:46 -0500
To: jug@solect.com
From: Neil Bartlett <neilb@the-wire.com>
Cc: java-interest@java.sun.com, john@insightnews.com
John, I am sending you this reply by multiple routes!! I have been having
trouble appending to the Toronto JUG mailing list - hence the redundancy :-)
At 11:34 PM 12/9/95 PST, you wrote:
>__Retrieving The File__
>I purposely accept the content [getContent()] into an Object instance
>because I'm not sure what class the content will be an instance of.
Quite right!!
>Since the Sun documentation says that the instanceof operator returns true
>if the object is an instance of the specified class, or an instance of a
>subclass, I figure that the test for 'if (graphData instanceof Object)'
>should display its message.
The documentation is correct. There is a wrinkle though - and it looks like
you might have hit it. The problem comes if graphData is null. In this case
things go awry. Use the format
if (x != null && x instanceof y)
when you do your checks.
The following code proves that all is correct with the instanceof operator
and shows how null screws things up.
------------- Test.java ------------------------------------------
class aclass {
}
class bclass extends aclass{
}
class Test {
static public void main(String args[]) {
aclass a = new aclass();
bclass b = new bclass();
// first of test classes as they are
System.out.println("The following should retun true");
System.out.println("b is of bclass ="+(b instanceof bclass));
System.out.println("b is of aclass ="+(b instanceof aclass));
System.out.println("a is of aclass ="+(a instanceof aclass));
System.out.println("a is of Object ="+(a instanceof Object));
System.out.println("b is of Object ="+(b instanceof Object));
System.out.println("The following should return false");
System.out.println("a is of bclass ="+(a instanceof bclass));
System.out.println("Some Tests based on assignment");
Object o = a;
System.out.println("o is of aclass ="+(o instanceof aclass));
System.out.println("o is of Object ="+(o instanceof Object));
System.out.println("Now assign call o to NULL and what what happens");
o = null;
System.out.println("o is of aclass ="+(o instanceof aclass));
System.out.println("o is of Object ="+(o instanceof Object));
}
}
------------------------------------------------------------------
Results
The following should retun true
b is of bclass =true
b is of aclass =true
a is of aclass =true
a is of Object =true
b is of Object =true
The following should return false
a is of bclass =false
Some Tests based on assignment
o is of aclass =true
o is of Object =true
Now assign call o to NULL and what what happens
o is of aclass =false
o is of Object =false
>
Neil Bartlett
Co-author of upcoming book, The Java Explorer
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com