<p>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 &amp; having a zero balance in ur account?</p>
<p>(The students may feel bad luck about 1st example, so I suggest u skip it unless too necessary)</p>

CC BY-SA 4.0
2021-06-17T07:03:30.487Z
2021-06-17T07:03:30.487Z
ShAr
<p>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 <code>optional</code> data types.  In modern C++, <code>std::optional&lt;foo&gt;</code> is the union of the <code>foo</code> data type with a special value that means &quot;no value,&quot; or &quot;not found,&quot; or &quot;invalid,&quot; or whatever the programmer wants it to mean.</p>
<p>IMO, you could teach that by imagining these two conversations:</p>
<blockquote>
<p>Q: How many apples are in your basket?
A: zero.</p>
</blockquote>
<pre><code>int numApplesIn(Basket basket);
std::cout &lt;&lt; &quot;I have &quot; &lt;&lt; numApplesIn(myBasket) &lt;&lt; &quot; apples.\n&quot;;
</code></pre>
<p>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.</p>
<blockquote>
<p>Q: What's the name of the biggest car dealership in this town?
A: Null. (Nobody sells cars around here.)</p>
</blockquote>
<pre><code>std::optional&lt;string&gt; findBiggestCarDealershipWithin(Municipality municipality);
auto dealer_name = findBiggestCarDealershipWithin(thisTown);
if (dealer_name.has_value()) {
    std::cout &lt;&lt; &quot;The bigest dealership is &quot; &lt;&lt; dealer_name &lt;&lt; '.\n';
}
else {
    std::cout &lt;&lt; &quot;Sorry, Nobody sells cars around here.\n&quot;;
}
</code></pre>
<p>The question asked for a single name, but there is <em>no name</em> that would be a truthful answer to the question. The function must be able to return a any valid name, <em>or</em> a special value that means &quot;no such dealer exists.&quot;</p>
<p>In a language that doesn't have <code>optional</code>, The function would written to return a reference to a string or a pointer to a string; and a <code>null</code> or <code>nil</code> reference/pointer would serve as the special value.</p>

CC BY-SA 4.0
2021-06-24T20:09:34.750Z
Java also has Optional. Its fully-qualified name is java.util.Optional.  There are also ad hoc Optional classes such as OptionalInt for primitives.
ncmathsadist
CC BY-SA 4.0
2021-06-17T14:23:07.207Z
2021-06-17T14:46:50.007Z
2021-06-17T14:46:50.007Z
Solomon Slow
<p>The idea of a <em>number</em> zero and null are orthogonal, actually. A machine (such as an automobile) can be in the forward, reverse or neutral(null) state. Perhaps &quot;off&quot; is a better metaphor for null here.</p>
<p>A contract can be &quot;in force&quot; or &quot;null and void&quot;.</p>
<p>Laws can be nullified by courts.</p>
<p>A win in an athletic contest can be made null by, for example, a doping agency.</p>
<p>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.</p>
<p>In the debit/credit example, the debit and credit are always &quot;in balance&quot;. Number can be introduced by saying that the &quot;net&quot; is zero.</p>
<p>While the two ideas are orthogonal, they might be said to intersect at zero.</p>

CC BY-SA 4.0
2021-06-17T19:45:31.050Z
2021-06-17T20:19:02.043Z
2021-06-17T20:19:02.043Z
Buffy
<p>I explain the concept by mysteriously showing this image as a prop:</p>
<p><a href="https://i.sstatic.net/NK3Qy.jpg" rel="noreferrer"><img src="https://i.sstatic.net/NK3Qy.jpg" alt="enter image description here" /></a></p>
<p>It always gets a laugh, which is nice :)</p>
<p>Though, really, this image (which I haven't found before) might lead to a richer discussion:</p>
<p><a href="https://i.sstatic.net/T9M2J.png" rel="noreferrer"><img src="https://i.sstatic.net/T9M2J.png" alt="enter image description here" /></a></p>
<p>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.</p>
<pre><code>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]);
</code></pre>

CC BY-SA 4.0
2021-06-17T14:54:59.243Z
2021-06-17T14:54:59.243Z
Ben I.
<h2>Option 1: Scores</h2>
<p>Arsenal is going to play Real Madrid tomorrow. What is their score today?</p>
<p>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 <strong>non-existant</strong>, which is what <code>null</code> represents.</p>
<p>In general, focus on examples where <strong>zero has meaning and is not synonymous with an absence</strong>. 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.</p>
<p>Even in the toilet paper answer Ben I posted here, I suspects students will still answer the question &quot;how many sheets are there on the roll?&quot; with 1/0/0/0 respectively, rather than failing to answer the question because there is no roll or no roll holder.</p>
<p>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 &quot;think&quot; they same way they do.</p>
<hr />
<h2>Option 2: Simulating a real-world exception</h2>
<p>The way I was taught <code>null</code> also worked well, in my opinion.</p>
<p>The teacher picked a volunteer, and told them the following:</p>
<blockquote>
<p>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.</p>
</blockquote>
<p>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 <strong>number</strong>.</p>
<p>When the student returned, their answer was, unsurprisingly:</p>
<blockquote>
<p>I couldn't find a piano in the storeroom.</p>
</blockquote>
<p>And this is in my opinion a really good example of what an exception (and in this particular case a null reference exception) is.</p>
<p>The teacher then posed some hypotheticals to the student.</p>
<blockquote>
<p>What would you have said if this was in the room?</p>
<p><a href="https://i.sstatic.net/ztn3qm.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ztn3qm.jpg" alt="enter image description here" /></a></p>
</blockquote>
<p>And the student said <strong>17</strong>.</p>
<blockquote>
<p>What would you have said if this was in the room?</p>
<p><a href="https://i.sstatic.net/t3rKCm.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/t3rKCm.jpg" alt="enter image description here" /></a></p>
</blockquote>
<p>And the student said <strong>0</strong>.</p>
<p>And then the teacher asked why he didn't say 0 when he walked in. The obvious answer being:</p>
<blockquote>
<p>Because there was no piano.</p>
</blockquote>
<p>It was one of the better ways I've seen <code>null</code> 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.</p>

CC BY-SA 4.0
2021-06-21T11:18:21.503Z
2021-06-21T11:29:39.740Z
2021-06-21T11:29:39.740Z
Flater
<p>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</p>

CC BY-SA 4.0
2021-06-20T08:45:26.273Z
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
CC BY-SA 4.0
2021-06-19T16:33:35.063Z
2021-06-19T16:33:35.063Z
lifeisaquestion
<p>What can be real-world examples to teach the difference between null and zero to high school students of computer science?</p>

CC BY-SA 4.0
2021-06-17T14:46:14.293Z
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.
CC BY-SA 4.0
2021-06-20T08:23:15.700Z
I once got a massive tax refund (that I later had to pay back), because I forgot to anwer the question about my income.
ctrl-alt-delor
CC BY-SA 4.0
2021-06-20T08:28:26.443Z
The invention of `null` was an error https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare/
ctrl-alt-delor
CC BY-SA 4.0
2021-06-30T23:11:46.363Z
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.
Steve
CC BY-SA 4.0
2021-07-01T08:49:39.467Z
I would like to point out that python is bit different in term of the concept null, it is None https://realpython.com/null-in-python/, which makes it easy to explain the difference between 0 and None (null).
Qiulang 邱朗
<secondary-education><teaching-analogy>
Real world examples for the difference between null and zero?
# Real world examples for the difference between null and zero?

(Asked by: username_0 on 2021-06-17)

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

**Tags:** secondary-education, teaching-analogy

> **Comments:**
> **username_1**: 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.  
> **username_2**: I once got a massive tax refund (that I later had to pay back), because I forgot to anwer the question about my income.  
> **username_2**: The invention of `null` was an error https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare/  
> **username_3**: 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.  
> **username_4**: I would like to point out that python is bit different in term of the concept null, it is None https://realpython.com/null-in-python/, which makes it easy to explain the difference between 0 and None (null).  

---

## 6 answers

### Answer by username_1 on 2021-06-17. Score: 7

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]);


---

### Answer by username_9 on 2021-06-21. Score: 3

## 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?
>
> [![enter image description here](https://i.sstatic.net/ztn3qm.jpg)](https://i.sstatic.net/ztn3qm.jpg)

And the student said **17**.

> What would you have said if this was in the room?
>
> [![enter image description here](https://i.sstatic.net/t3rKCm.jpg)](https://i.sstatic.net/t3rKCm.jpg)

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.


---

### Answer by username_10 on 2021-06-19. Score: 3

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

> **Comments:**
> **username_5**: 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  

---

### Answer by username_5 on 2021-06-17. Score: 2

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)


---

### Answer by username_6 on 2021-06-17. Score: 0

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 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:

Q: How many apples are in your basket?
A: zero.

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 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.

> **Comments:**
> **username_7**: Java also has Optional. Its fully-qualified name is java.util.Optional.  There are also ad hoc Optional classes such as OptionalInt for primitives.  

---

### Answer by username_8 on 2021-06-17. Score: 0

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.


---

