Is null an Object?

2019-01-04 19:22发布

Is null an Object in Java?

标签: java null
15条回答
我命由我不由天
2楼-- · 2019-01-04 19:24

Is null an instance of java.lang.Object? No.

Is null an object? depends on the definition of "is".

查看更多
Summer. ? 凉城
3楼-- · 2019-01-04 19:24
Object foo = null;
System.out.println(foo.toString()); 

The first line shows null can be assigned to type Object, but the second line will demonstrate it is certainly not an Object and eventually results in a java.lang.NullPointerException

查看更多
【Aperson】
4楼-- · 2019-01-04 19:27

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.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-04 19:28

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 literal null provided by the compiler.

查看更多
家丑人穷心不美
6楼-- · 2019-01-04 19:28

JRL wrote:

No it's not, ...

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 type Object?”. In addition to JLS 4.1 cited by Michael Borgwardt above:

See JLS 3.10.7:

A null literal is always of the null type.

and JLS 4.10:

The subtypes of a type T are all types U such that T is a supertype of U, and the null type.

or JLS 4.10.2:

The direct supertypes of the null type are all reference types other than the null type itself.

[Emphases by me.]

According to Eclipse Juno's compiler it's not:

true.toString();  // Cannot invoke toString() on the primitive type boolean
null.toString();  // Cannot invoke toString() on the primitive type null

According to JDKs 1.7.0_07 javac it is:

true.toString();  // error: boolean cannot be dereferenced
null.toString();  // error: <null> cannot be dereferenced

Where the angle brackets imply for me that null is of an other than a primitive type. And according to JLS 4.1:

There are two kinds of types in the Java programming language: primitive types and reference types.

So, if it's not the one it's the other.


Claudiu wrote:

null is kind of ugly.

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:

null is a type and a value.

There are actually three items in conjunction with null (see also JLS 3.10.7):

  1. The (otherwise unnamed) null type.
  2. The null literal.
  3. The null reference value. (Commonly abbreviated as null value or simply null.)

(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:

[...] while null might appear to be a keyword, it is technically the null literal.


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 to true, shouldn't it? At first sight, yes, but JLS 15.20.2 gives the insight answer:

[...] the result of the instanceof operator is true if the value of the RelationalExpression is not null [...]. Otherwise the result is false.

[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 us

  • or 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:

     RedBullStratosBalloon balloon = null;
     boolean b = balloon instanceof Aircraft;  // True? There's not even an instance
                                               // which could be of type Aircraft.
    

Which also leads to:

Why is instanceof not a proper way to say something about null's Object-ness?

It's called instanceof not sameorsubtypeof. That means we are comparing an instance's type with a type, not two types. Now null 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 to false.

Or in a "more" real world example:

  • I have a real-size picture of an apple (=reference type) in my hands with »Big Apple« (=reference type name) written on it.
  • There's a table (=heap) in front of me.
  • If there is an apple (=instance) on the table there is a cord (=reference) connected to it.
  • I hold the other end of this cord in my hand (=reference variable) .
  • I trace the apple along the cord and compare it with my picture (=instanceof).
  • If the apple is of the same size or bigger than the picture the writing »Big Apple« applies to it (=true).
  • If it's smaller, then not (=false).
  • If there is no apple on the table and, hence, no cord exists (=null) the writing doesn't apply either (=false). Because: Is no apple a big apple? No, it's not.


As Michael sums up: "null is special" indeed.

查看更多
乱世女痞
7楼-- · 2019-01-04 19:32

No, is not an object as null instanceof Object will always return false also there is only one null, not one for each class.

查看更多
登录 后发表回答