views:

102

answers:

5

Guys, could any one give a logical explanation of phrase I met in this book:

You may find it helpful to think of ? extends T as containing every type in an interval bounded by the type of null below and by T above (where the type of null is a subtype of every reference type).

Thanks.

+4  A: 

I think it just means that you can assign the null reference to any reference type. It doesn't strike me as a terribly helpful way of thinking about it.

The Java Language Specification has this to say about the null type (in section 4.1):

There is also a special null type, the type of the expression null, which has no name. Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type. The null reference is the only possible value of an expression of null type. The null reference can always be cast to any reference type. 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.

Jon Skeet
@Jon: so, the key phrase here is `The null reference can always be cast to any reference type`, this what makes auhthor to claim `the type of null is a subtype of every reference type`. Weird one :) Thanks for pointing this.
Max
It's a helpful way of thinking insofar as it tells you that Java's type system is unsound, which in turn makes it pretty much useless.
Jörg W Mittag
A: 

That means that null can be any type.

You might have a class Animal with a subclass Dog. If you have a variable of type Animal with the value null you can not tell if the null is of type Animal or type Dog. Both are equally valid.

Albin Sunnanbo
I think that's a slightly deceptive statement. The value `null` always has exactly one type - the type `null`. It cannot be "any type". The type system defines the type `null` (which is only inhabited by the value `null`) to be a sub-type of all reference types. In your example, this is as though you had an implied `null` class defined as a sub-class of `Animal`, and therefore it would be valid to assign the value `null` (an instance of this `null` class) to a variable of type `Animal`.
Gian
A: 
null instanceof T

is true for any type T.

Ladlestein
@Ladlestein: how about `System.out.println(null instanceof Object);` giving `false`?
Max
@Ladlestein Thats completely not true. Java creators went out of their way to make that case always false
John V.
I am embarassed. I just assumed!
Ladlestein
+2  A: 

Types form a partial order, i.e. for any two types in the set of all types in the program, there might be a relation between them (i.e. T1 < T2 - T2 is a subtype of T1 in some sense). Things in unrelated class hierarchies have no such relation defined.

So basically what this is telling you is that all members of the set of types that ? extends T describes is less than T and greater than null. null exists as a subtype of everything because it is always valid to assign the value null to a reference.

More formally:

∀x ∈ ? extends T. null ≥ x ≥ T
Gian
+2  A: 

From this interesting article, an excerpt:

Java has the null type. Pre-JLS3, the null type was not officially the subtype of any type, and the null reference was not officially a value of any type except the null type. A decree made the null reference castable to any reference type for pragmatic reasons. (This is similar to the decree that makes List assignable to a List formal parameter even though List is not a subtype of List. You know the decree as capture conversion.) JLS3 defines the null type as a subtype of every type, so it looks an awful lot like Bottom.

JRL
@JRL: Appreciate a lot your elaboraton of the topic. I wasn't aware of that monster null type.
Max
If you know Scala, the type of Java's `null` is similar to the Scala type `Nothing`, which is a subtype of all reference types (all types that are subtypes of `AnyRef`).
Jesper