Real world examples for the difference between null and zero?

What can be real-world examples to teach the difference between null and zero to high school students of computer science?

• I was completely sure that this was on our site already, but I can't find it, so I'm going to just leave an answer here. If someone else finds it, we can point this question to that one.
– Ben I.
Jun 17 at 14:46
• I once got a massive tax refund (that I later had to pay back), because I forgot to anwer the question about my income. Jun 20 at 8:23
• The invention of null was an error infoq.com/presentations/… Jun 20 at 8:28
• Null doesn't have a single consistent definition in all cases, but the most straightforward analogy is the difference between writing "0" on a page, and writing nothing. Jun 30 at 23:11
• I would like to point out that python is bit different in term of the concept null, it is None realpython.com/null-in-python, which makes it easy to explain the difference between 0 and None (null). Jul 1 at 8:49

I explain the concept by mysteriously showing this image as a prop:

It always gets a laugh, which is nice :)

Though, really, this image (which I haven't found before) might lead to a richer discussion:

What I follow with is examples with arrays, along the lines of the following, and have students discuss in small groups what will work and what won't. We then go over it as a whole group.

int[] arr;
System.out.println(arr);
System.out.println(arr.length);
System.out.println(arr[0]);

int[] arr = null;
System.out.println(arr);
System.out.println(arr.length);
System.out.println(arr[0]);

int[] arr = new int[0];
System.out.println(arr);
System.out.println(arr.length);
System.out.println(arr[0]);

int[] arr = new int[1];
System.out.println(arr);
System.out.println(arr.length);
System.out.println(arr[0]);


When you fill a form, and in the AGE field, you leave it empty THAT IS NULL. If you instead of leaving it empty, you fill it with 0 (zero) THAT IS ZERO

• I think u mean if an application doesn't offer the Null option in the age field when it's irrelevant & some users don't want to write it, u would be like forcing them to write zero. Now if the application uses this data in some kind of statistic gathering it may lead to wrong results, if u care to know the av age of ur customers for ex., or even worse if u r an automated system describing medical dosage according to age (a just born baby do have an age zero if the database involve hospitals). So, this is really a good ex for Developers on the importance of offering the Null option
– ShAr
Jun 20 at 8:45

Option 1: Scores

Arsenal is going to play Real Madrid tomorrow. What is their score today?

Focus on the difference between a match which concluded 0-0 and a match which has not been played yet. For the latter, the score is non-existant, which is what null represents.

In general, focus on examples where zero has meaning and is not synonymous with an absence. A score is a great example because a 0 is a meaningful value which indicates an achieved result, as opposed to not having a result.

Even in the toilet paper answer Ben I posted here, I suspects students will still answer the question "how many sheets are there on the roll?" with 1/0/0/0 respectively, rather than failing to answer the question because there is no roll or no roll holder.

Human minds can account for badly posed questions in a way that logical algorithms cannot, and this is precisely why it's so hard for your students to understand that a compiler does not "think" they same way they do.

Option 2: Simulating a real-world exception

The way I was taught null also worked well, in my opinion.

The teacher picked a volunteer, and told them the following:

I want you to go to the end of the hall, in the storeroom. There is a piano in there. I want you to tell me how many keys it has. When you return and walk in, give me the answer, nothing more.

When the student left, the teacher discussed with the class what they expected his answer to be. The main point to establish here was to get the class to agree that they were going to respond with a number.

When the student returned, their answer was, unsurprisingly:

I couldn't find a piano in the storeroom.

And this is in my opinion a really good example of what an exception (and in this particular case a null reference exception) is.

The teacher then posed some hypotheticals to the student.

What would you have said if this was in the room?

And the student said 17.

What would you have said if this was in the room?

And the student said 0.

And then the teacher asked why he didn't say 0 when he walked in. The obvious answer being:

Because there was no piano.

It was one of the better ways I've seen null explained to a bunch of laymen. It still sticks with me to this day, while also (tangentially) explaining exactly what the purpose of an exception is as opposed to a return value.

Maybe didn't attend an exam, or register in the course, as apposed to took zero? -Or the difference between not having an account in a certain bank & having a zero balance in ur account?

(The students may feel bad luck about 1st example, so I suggest u skip it unless too necessary)

The possibility of a null pointer or a null reference is the soon-to-be-old-fashioned way to express what some programming languages represent with optional data types. In modern C++, std::optional<foo> is the union of the foo data type with a special value that means "no value," or "not found," or "invalid," or whatever the programmer wants it to mean.

IMO, you could teach that by imagining these two conversations:

int numApplesIn(Basket basket);
std::cout << "I have " << numApplesIn(myBasket) << " apples.\n";


The question asked for a number, and it can always be answered with a number. Zero is a number--a valid answer to the question.

Q: What's the name of the biggest car dealership in this town? A: Null. (Nobody sells cars around here.)

std::optional<string> findBiggestCarDealershipWithin(Municipality municipality);
auto dealer_name = findBiggestCarDealershipWithin(thisTown);
if (dealer_name.has_value()) {
std::cout << "The bigest dealership is " << dealer_name << '.\n';
}
else {
std::cout << "Sorry, Nobody sells cars around here.\n";
}


The question asked for a single name, but there is no name that would be a truthful answer to the question. The function must be able to return a any valid name, or a special value that means "no such dealer exists."

In a language that doesn't have optional, The function would written to return a reference to a string or a pointer to a string; and a null or nil reference/pointer would serve as the special value.

• Java also has Optional. Its fully-qualified name is java.util.Optional. There are also ad hoc Optional classes such as OptionalInt for primitives. Jun 24 at 20:09

The idea of a number zero and null are orthogonal, actually. A machine (such as an automobile) can be in the forward, reverse or neutral(null) state. Perhaps "off" is a better metaphor for null here.

A contract can be "in force" or "null and void".

Laws can be nullified by courts.

A win in an athletic contest can be made null by, for example, a doping agency.

Most ancient cultures had no concept of zero as a number. There is no Roman Numeral for zero, of course. Money owed is a credit to one party and a debit to the other, but zero has no place since there is nothing owed.

In the debit/credit example, the debit and credit are always "in balance". Number can be introduced by saying that the "net" is zero.

While the two ideas are orthogonal, they might be said to intersect at zero.