Is null an Object
in Java?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Is null an instance of
java.lang.Object
? No.Is null an object? depends on the definition of "is".
The first line shows
null
can be assigned to typeObject
, but the second line will demonstrate it is certainly not anObject
and eventually results in ajava.lang.NullPointerException
Java handles objects via references. Null is a breakdown of OO-ness of Java, since it drops you below OO level. No it is not an object it is a VALUE of a reference. And it has nothing to do with object paradigms, but relates to plumbing of Java, that enables objects.
According to the Java spec,
null
is a type that can be assigned to an object variable (as a value as noted in the comment). You cannot instantiate or create variables of this type though, you must use the literalnull
provided by the compiler.JRL wrote:
As often, it depends from where you look at it, who you believe more.
According to the JLS, yes, it is. Especially if you rephrase the question to: „Is the
null
literal of typeObject
?”. In addition to JLS 4.1 cited by Michael Borgwardt above:See JLS 3.10.7:
and JLS 4.10:
or JLS 4.10.2:
[Emphases by me.]
According to Eclipse Juno's compiler it's not:
According to JDKs 1.7.0_07
javac
it is:Where the angle brackets imply for me that
null
is of an other than a primitive type. And according to JLS 4.1:So, if it's not the one it's the other.
Claudiu wrote:
Au contraire,
null
is beautiful. What would you suggest as default value for a reference type variable instead? An arbitrary bit combination? Welcome to access violation or, even worse, pointer hell!Joachim Sauer wrote:
There are actually three items in conjunction with null (see also JLS 3.10.7):
null
literal.(1) Note that, according to JLS 4.10.2 cited above, the null type uses multiple inheritance not only for interfaces but for classes as well. Which we all know is not possible for us application programmers.
(2) The null literal might be imagined as a variable being defined as:
JVM_global final null_type null = new null_type();
Note also JLS 3.9:
Concerning
null instanceof <anytype>
:With JLS 4.10.2 in mind („the null type is a subtype of every type”)
null instanceof <anytype>
should be supposed to evaluate totrue
, shouldn't it? At first sight, yes, but JLS 15.20.2 gives the insight answer:[Emphases by me.]
Ask yourself what makes more sense (from the application programmer's point of view):
Giving
false
and thus indicating that a reference expression is not of a type exposed to us, i.e. indicating it's not referencing anything useful to usor giving
true
, thus informing us that the expression evaluates to a special reference, the null reference, referencing an "object" we don't know whether it even exists and which is of the special null type which has no name, is not exposed to us but via the null literal, is a subtype of any type including multiple inheritance and is to be ignored anyway? Consider also the more practical example:Which also leads to:
Why is
instanceof
not a proper way to say something aboutnull
's Object-ness?It's called
instanceof
notsameorsubtypeof
. That means we are comparing an instance's type with a type, not two types. Nownull
means: „There is no instance” and if there is no instance there's no instance's type. It's obvious that comparing nothing with something is supposed to lead tofalse
.Or in a "more" real world example:
As Michael sums up: "null is special" indeed.
No, is not an object as null instanceof Object will always return false also there is only one null, not one for each class.