1

I am going through "The Java Handbook" by Patrick Naughton.

Book says: When you declare the type of a variable to be a class, it has a default value of null which is a reference of type Object, and is thus type-compatible with all the other classes. The null object has no value; it is distinct from the integer 0, just like boolean's false.

I cannot understand/confuse the highlighted part, does it say that null is a variable of type Object?

Page no: 107 para: 5

Sushant Kr
  • 1,953
  • 14
  • 19

2 Answers2

7

It's a reference value, not a reference variable. Any variable that is a reference type can have the value null. (In Java, all variables are either reference types or primitive types (int, char, float, etc.). (Well, there are also type variables when you start to talk about generics.)

Here's the relevant part of the Java Language Specification:

There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3). There are, correspondingly, two kinds of data values that can be stored in variables, passed as arguments, returned by methods, and operated on: primitive values (§4.2) and reference values (§4.3).

There is also a special null type, the type of the expression null (§3.10.7, §15.8.1), 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 undergo a widening reference conversion 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.

EDIT To address your comment: I think the language in "The Java Handbook" is a little off the mark. The value null is not a reference of type Object; it is a reference of the null type. The key point from the spec is that "the null reference can always undergo a widening reference conversion to any reference type". This is, in a sense, exactly the opposite of Object. An Object reference is the widest type of reference; the null type is (to also speak a little loosely) the narrowest. In particular, assigning a reference of type Object to a variable of any other reference type is a narrowing conversion that requires an explicit cast (and can raise a ClassCastException). Assigning the null reference to a variable of any reference type never requires a cast and cannot raise an exception.

Note that no named reference type can have the behavior of the null type. There really is no "narrowest type" since the (named) type system in Java does not not have such a thing. It is impossible to define a reference type that is assignable to, say, both a String variable and a Double variable. Only the null type has that property. The normal rules for reference type conversion do not allow this, which is why it has a separate rule in the Java Language Specification.

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Thank-u....your answer makes perfect sense, but if type of null expression is null...why does its says that "null which is a reference of type Object"!? where does Object come into picture? – Sushant Kr Nov 18 '12 at 00:11
  • 1
    @SushantKr - I updated my answer to respond to your comment. – Ted Hopp Nov 18 '12 at 03:48
1

From JLS:

The null type has one value, the null reference, represented by the null literal null, which is formed from ASCII characters.

PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • Thank-u....your answer makes perfect sense, but if type of `null` expression is null...why does its says that "null which is a reference of type Object"!? where does `Object` come into picture? – Sushant Kr Nov 18 '12 at 00:33
  • @SushantKr every object in java has default value as null. Object in this case could be any Object, even the objects you create, String, Object, or whatever .. :) – PermGenError Nov 18 '12 at 00:37
  • **which is a reference of type Object, and is thus type-compatible with all the other classes**, if I am not wrong "which" refers to 'null'? – Sushant Kr Nov 18 '12 at 00:54
  • @SushantKr object type(any object like string....) refers to null, that is why you get a NPE when you invoke objects behavior (methods) when not initialized(when the object has its default value, which is null) – PermGenError Nov 18 '12 at 00:58