[4082] in java-interest

home help back first fref pref prev next nref lref last post

instanceof operator playing with my mind

daemon@ATHENA.MIT.EDU (John D. Kane)
Sun Dec 10 01:02:08 1995

From: "John D. Kane" <john@insightnews.com>
To: "'java-interest@java.sun.com'" <java-interest@java.sun.com>,
        "'jug@solect.com'" <jug@solect.com>
Date: Sat, 09 Dec 95 23:34:00 PST


I'm attempting to retrieve a text file from the server through an applet 
running on the client. The following excerpt of applet coding works 
correctly; it does not throw any exceptions (I left out the exception 
handling code because it makes everything look too complicated).

public class ... ... ... {

   URL graphDataLoc;
   URLConnection availableGraphData;
   Object graphData

   public void init() {
      // retrieving the file
      graphDataLoc = new URL(getCodeBase(), "Data/StockInfo.TXT");
      availableGraphData = graphDataLoc.openConnection();
      graphData = availableGraphData.getContent();

      // testing content type using instanceof operator
      if (graphData instanceof Object)
         System.out.println("an instanceof Object");
      if (graphData instanceof File)
         System.out.println("an instanceof File");
      ... ... // and other tests using instanceof operator
   }
...
}

__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.

__Testing using instanceof operator__
Through a series of instanceof tests, I try to determine what class this 
object might be an instance of. In my original coding, I've tested at least 
a dozen different classes which seem resonable candidates. However, nothing 
is ever displayed to the java console meaning that the object is not an 
instance of any of the classes I've tested.
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.

I then decided to write a small applet that creating an instance of an 
image. I then tested it with 'if (imageInst instanceof Image) 
System.out.println("true");' This worked ok. The given message appeared in 
the java console. So now I'm baffled because
my above code seems to indicate that graphData, an instance of Object, is 
really an instance of nothing. I even tried 'if (graphData instanceof 
NULL)', but got a null pointer exception message on the java console.

This is my first time playing around with the instanceof operator. Can 
anyone give me any Insight as to why the instanceof tests in the above code 
seem to be playing games with me? Thank you.

                                   John
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post