[599] in java-interest
null objects and the instanceof operator
daemon@ATHENA.MIT.EDU (Nathan J. Williams)
Wed Jul 5 21:41:07 1995
Date: Wed, 5 Jul 1995 21:33:28 -0400
From: "Nathan J. Williams" <nathanw@ai.mit.edu>
To: java-interest@java.Eng.Sun.COM
In the process of debugging a program, I discovered that a
null object handle was considered to be an instance of a normal class
by the instanceof operator. Further testing revealed that any null
handle is considered an instanceof any class.
The language spec says that null "can be cast to any class
type", but does this imply that it is automatically considered any
type? I don't see anything in the spec that indicates the behavior.
For consistency, null needs to either be an instance of every
class or of no class. I would prefer that null be an instance of no
class. It is, in a sense, the language's only "non-object". More
importantly, when the test:
(a instanceof classB)
is true, I expect to be able to perform a.classBmethod1() or
look at a.classBvariable2, not raise a NullPointerException.
Is there some reason that null must be an instance of every
class? If not, I would recommend that this behavior be changed so
that null is not an instance of any class, and I would also recommend
that whichever way this is settled, that the behavior be documented
and defined by the language spec.
- Nathan Williams
<nathanw@mit.edu> http://web.mit.edu/nathanw/www/Welcome.html
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com