Wednesday, September 06, 2006

null in Java

In Java, null is defined as a reference literal, a reserved word. It looks like a keyword, but technically, it is not.

The reason is actually simple. Let's get back to programming fundamentals. Keyword is defined in a programming language so that it is special to the compiler. For Java compiler, null is just a literal and thus null is not a keyword. So are true and false. They are boolean literals other than keywords.

The following quote comes from Sun documents. It will help you understand more.
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.

No comments: