What is null? There’s no easy answer. If this essay were an episode of Seinfeld, it would be called “The Null Column,” because it’s a column about nothing—literally. Upon further inspection, however, null is not nothing. That’s what void is for. In fact, void is a much clearer concept than null. void is pure. void is simple. void is loyal and unwavering. But void is not null. If null were on Facebook, its relationship status would be “it’s complicated” (see Figure 1).
Figure 1. Null is a complicated concept.
In the excellent book Zero: The Biography of a Dangerous Idea, which I highly recommend, Charles Seife describes zero as a once-evil concept that was missing from ancient number systems. The idea of zero or nothingness (sounds like null to me) is one of the greatest paradoxes of human thought, according to the author. Its invention and acceptance led to other great discoveries such as fractions and even calculus.
On UNIX, null can be a device, as in /dev/null, where it acts like a black hole when written to; nothing ever comes out. This contrasts with a null modem cable, where all the data certainly does come out; it’s just crossed over electrically.
In common vernacular, some things can be “null and void” at the same time, such as a clause in a contract, a discount offer used on the wrong day or for the wrong person. But that concept never applies in a programming language: There, nothing is both null and void.
Maybe null was best explained in my favorite movie, Tron, During a scene with the evil Sark and one of his underlings, in a fit of anger and disappointment, Sark calls the errant program “Null Unit.” By this, Sark implies emptiness, absence of intelligence, disappointment, or just plain frustration.
Looking back, I started programming at a young age with TI-BASIC, then Commodore BASIC, and then 68000 Assembly for my Amiga (I couldn’t afford a C compiler at the time). None of those has the concept of null. But it was there in spirit because null is a notion, a concept, an abstraction. It’s like love: You cannot precisely define it, but you know it when you have it. Heck, like love, null even causes a feeling, such as that sinking feeling you get when your program crashes from a null pointer (I mean a null reference).
The polyglot null
For programming languages, null means different things depending on the language, and there are different sets of rules that govern how it’s used. For instance, in C, null is zero. Even Brian Kernighan and Dennis Ritchie’s The C Programming Language uses 0 in place of null (well, NULL) in some code examples.
char* alloc(int n) {
if ( allocbuf + ALLOCSIZE - allocp >= n ) {
allocp += n;
return allocp - n;
} else /* no room */
return 0;
}
Null is simply defined as zero in a macro.
#define NULL 0
In C, null is also confused with false. The following code compiles:
bool b = false;
int y = 0;
if ( !b && !y ) {
return 1;
}
return 0;
The snippet above outputs the following:
RUN FINISHED; exit value 1
Perhaps it’s a null pointer constant? Even this C code compiles.
int main(int argc, char** argv) {
int* p = 0;
int q = NULL;
return q;
}
Yes, the above code may generate a warning, but when it runs, the program exits with the value zero.
RUN FINISHED; exit value 0;
That’s confusing!
In JavaScript, null is null. In fact, you can call it NULL, or Null, or Nil, but don’t call it undefined. That’s an entirely different thing unless you’re checking with == (double equals signs) instead of === (triple equals signs). In this context, let’s simply call null “null-ish.”
Python looks promising, as it uses None to denote null, which at first makes sense. Yet when you see how None behaves, you realize it, too, will let you down. For example, setting a function return to None does what you’d expect: It returns nothing. But if you print the value of a function that returns None, you get the following:
>>> eric=None
>>> print(eric)
None
In Python, printing the value None gives you something, in the form of text that spells out the word None. That’s irony at the level of genius.
What about Java?
Surely Java gets null right, right? Of course, in Java, null is a null reference—except when null is a null pointer.
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.toString()" because "ref" is null
com.ericbruno.nullreference.Test.main(Test.java:11)
Nevertheless, null means something in Java. It’s not void. If your method is defined with a void return, you can’t return anything. Yet if your method is declared to return an Object, you can never return void, but you sure can return null. (You can also return an actual Object, if you want to be boring.)
And I recently learned from Bruce Eckel that null can even be a legitimate case in a Java switch statement.
More precisely, null is not a Java keyword, but instead it is a literal in Java.
According to the Java Language Specification, a literal is a representation of a primitive type, a String type, or the null type.
As the specification states, the null type has one value: the null reference, represented by the literal null. The null type, in contrast, is nameless. As you likely know already, the null literal can be assigned only to reference types, not to primitive types. Although you can cast a null reference to any reference type in Java, the return from instanceof is always false, meaning null is null in Java.
public class Test {
public static void main(String[] args) {
Integer i = (Integer)null;
if ( i instanceof Integer ) {
System.out.println("i is of type Integer");
}
}
}
The result of the code above is no output. Take that, Python!
Source: oracle.com
0 comments:
Post a Comment