madbean

There is no object. There is no null.

24 Jun 2003

Jeremy's entry on NullPointerException and pointers reminded me of a discussion I had while once teaching someone the inner "zen" of Java. (BTW, Charles has some interesting historical tidbits on null and some good tips to follow.)

There is no object

So, we were looking at this Java code:

void spoon(Object bar) {
  bar = "world";
}

int i;
i = 42;
Object foo = "hello";

spoon(foo);

and the student said "foo is the string object 'hello'."

I replied, "No, foo is a variable. It is a reference variable. It currently references the string object 'hello'. No variable is an object."

And the student was like, "WTF?!"

I continued, "foo is a variable. Like any variable, all it can do is have a value1. The possible values for foo are 1) a reference (pointer) to a java.lang.Object (or subclass), or 2) null."

"In addition," I added, "arguments to method calls are passed by value."

"But," the student replied, "in the call to spoon(), the string object 'hello' is passed by reference and assigned to the parameter bar."

"No," I corrected, "foo is a variable that has a value. That value is passed, by value, to spoon() and that value is assigned to bar. So, bar will have the same value as foo -- a reference to the string object 'hello' -- but bar is not a reference to foo. Does changing bar change foo?"

"Spud, like, WTF are you going on about"

Okay, so I'm trying to have a little fun with the koan form. The upshot is:

  • In Java, you don't play with objects. Just with values and references.
  • In Java, it's pass by value. A reference is a value. There is no object.

There is no null

In the Dylan2 language, there is no null. No nil, no None, no nada.

It is great when you don't have to worry about null. If you call a method on some variable foo, you never have to check if it's null.

But no-null may seem like a huge inconvenience. For example, how do you represent the "end" pointer in a linked list. Well, in Dylan, if a reference needs be two different things -- a next pointer can point to another node, or it can be 'nothing else' -- then you are forced to create a union-type. You are forced to declare it as two different things.

The 'false' value is often used this way. So we would declare the 'next' pointer as a reference to a node, or 'false':

define class <node> (<object>)
  slot next: false-or(<node>)3;
end class;
instead of
define class <node> (<object>)
  slot next: <node>;
end class;

But then again, Dylan is pretty wacky.


1 Mumon's Comment: ... all it can do is have a value, or be as-yet unassigned.

2 A good place for Dylan info is at the Gwydiondylan page, especially their learning page.

3 false-or() is a convenience macro for creating a type-union with 'false'.

  • Home
  • Blog