3

In jls-4.1 it's said that

There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3)

and

There is also a special null type

So basically null type is neither primitive nor reference type.

But why can it hold the reference and can always undergo a widening reference conversion to any reference type if it's not considered a reference type?

Or it is some kind of reference type in the end? Can it be considered as reference type?

So, what's the answer to the question - Is [null] a reference type or not - should be? Yes or No?

Eran
  • 387,369
  • 54
  • 702
  • 768
lapots
  • 12,553
  • 32
  • 121
  • 242

5 Answers5

6

First of all, null is not a type but a value. Variables whose type is a reference type might have the value null, but you can’t use null at places, where a reference type is expected, you can’t declare a variable of type null and you can’t have a type parameter null, e.g. you can’t declare a List<null>.

The value null has a special “null type” that is only relevant during compilation (and verification) to be able to formulate the formal rule that the “null type” is assignable to a reference type. But the “null type” is not a reference type.

To add some quotes from the specification:

JLS §4.1:

There is also a special null type, the type of the expression null (§3.10.7, §15.8.1), which has no name.

The null reference can always be assigned or cast to any reference type (§5.2, §5.3, §5.5).

In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.


JVMS §4.10.1.2:

  Verification type hierarchy:

                             top
                 ____________/\____________
                /                          \
               /                            \
            oneWord                       twoWord
           /   |   \                     /       \
          /    |    \                   /         \
        int  float  reference        long        double
                     /     \
                    /       \_____________
                   /                      \
                  /                        \
           uninitialized                    +------------------+
            /         \                     |  Java reference  |
           /           \                    |  type hierarchy  |
uninitializedThis  uninitialized(Offset)    +------------------+  
                                                     |
                                                     |
                                                    null
Community
  • 1
  • 1
Holger
  • 285,553
  • 42
  • 434
  • 765
  • Wow.. Its a pity only one upvote is allowed per answer.. :) – TheLostMind Oct 29 '14 at 14:19
  • In other languages there is the "bottom" type which is defined as a subtype of all reference types. Java didn't choose this approach, perhaps because Java's approach is simpler. – Marko Topolnik Oct 29 '14 at 18:56
5

Well, it also says In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.

Therefore it can be considered as a value that can be assigned to any reference type.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • But I'm not talking about practice. So it is not some kind of reference type in the end? – lapots Oct 29 '14 at 13:57
  • @user1432980 - It doesn't matter. As *Marko Toplonik* points out in his answer.. The behavior is important, not the *semantics*. – TheLostMind Oct 29 '14 at 14:04
  • 1
    @user1432980 The JLS says it isn't, but whether it is or is not a reference type makes no practical difference. – Eran Oct 29 '14 at 14:06
3

null type can be considered as a reference type because you can have Object o = null whereas int i = null will give a compilation error. Actually, null is merely a placeholder to say nothing. In this case, your object points to nothing.

So, if you call o.toString(), you will get a NullPointerExeption because you are trying to call a method on nothing.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • @Holger - Your code will give an NPE.. The compiler doesn't allow us to *directly* assign a null value to a primitive type. This proves that null is *incompatible* with a primitive type.. In your example, you are *hoodwinking* the compiler. – TheLostMind Oct 29 '14 at 14:32
  • but being incompatible with a primitive type does not prove being a reference type. By the way, `int i=true? 42: null;` will work without throwing an NPE. I only wanted to emphasize that producing a compiler error or not, is not a real prove for a formal rule. – Holger Oct 29 '14 at 14:39
  • @Holger - Then why do you think primitive types cannot be allowed to refer to `null` ? – TheLostMind Oct 29 '14 at 14:43
  • I think you know the answer. There is no place within the value set of a primitive value to contain a `null` value, e.g. all 2³² possible values of an `int` have a valid numerical value. And the primitive values were created for efficiency, to be closer to how the CPU works and that precludes processing a possible `null` state. The formal rule using a “null type” was created to justify that pragmatic decision with a formal rule. A correct way to say it could be “variables of reference types can have a `null` value” but a type system needs a place-holder type to say that formally. – Holger Oct 29 '14 at 14:51
2

You have quoted the parts of the specification which clearly spell out that null is not a reference type. The value of a reference type is always a reference and specifying null as a reference type would probably create the need to introduce many special-case stipulations around the JLS.

However, it would be most prudent of you to "unask" your entire question because it is immaterial to any practical aspect of Java programming and is only of interest in the context of JLS's internal definitions. If that kind of question ever arises on an exam, you should blame the author for asking it. As an interview question it may work only as a conversation starter.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
1

null is generally considered more as a place-holder to to set Object references to ,well null to make the Objects eligible for Garbage collection etc, it has a lot more uses too...coming back to your Qeeston Yes null can be considered a Special reference type too

Sainath S.R
  • 3,074
  • 9
  • 41
  • 72