This was intended as a meme but is actually a good representation of what "Null" is. In C#, when you declare string s = "My shit"; it means that "s" is a reference to a memory location that holds the data "My shit". string s = null; means that the reference "s" exists but it's not pointing to any object, as in it holds nothing. Answer from abd53 on reddit.com
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Null
Null - Glossary | MDN
In computer science, a null value represents a reference that points, generally intentionally, to a nonexistent or invalid object or address. The meaning of a null reference varies among language implementations.
Discussions

language agnostic - What is the purpose of null? - Stack Overflow
But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years. In recent years, a number of program analysers like PREfix and PREfast in Microsoft have been used to check references, and give warnings if there is ... More on stackoverflow.com
🌐 stackoverflow.com
Why is there a NULL in the C language? - Stack Overflow
The use of NULL is not just “preferred,” and this does not really answer the question the OP asked, “Is there a context in which just plain literal 0 would not work exactly the same?” That context is where an error such as where x is an int * and the programmer accidentally typed *x ... More on stackoverflow.com
🌐 stackoverflow.com
What's the difference between Null, NA, #NULL, nothing, ""
I am a bit lost on the many ways to represent the “empty” concept in Julia… What is the difference between Null (from Nulls.jl), NA (From DataFrames), #NULL from NullableArrays, Nullable container (from base), nothing (in base) and the empty string "" ? More on discourse.julialang.org
🌐 discourse.julialang.org
1
2
October 16, 2017
programming languages - If null is a billion dollar mistake, what is the solution to represent a non-initialized object? - Software Engineering Stack Exchange
An Optional is a type which contains ... to happen when no value is present (again: do nothing, return a placeholder or throw a specific exception). Programming such a class in such a language would of course be impossible without also implementing a null-object, which you are then ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
July 26, 2020
🌐
GeeksforGeeks
geeksforgeeks.org › java › interesting-facts-about-null-in-java
Interesting facts about null in Java - GeeksforGeeks
September 3, 2024 - Below are some important points about null in java that every Java programmer should know: In Java, null is a special value that represents the absence of a value or reference.
🌐
Esolang
esolangs.org › wiki › NULL
NULL - Esolang
August 29, 2024 - NULL is a programming language in zero dimensions.
Top answer
1 of 16
49

Null: The Billion Dollar Mistake. Tony Hoare:

I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years. In recent years, a number of program analysers like PREfix and PREfast in Microsoft have been used to check references, and give warnings if there is a risk they may be non-null. More recent programming languages like Spec# have introduced declarations for non-null references. This is the solution, which I rejected in 1965.

2 of 16
32

null is a sentinel value that is not an integer, not a string, not a boolean - not anything really, except something to hold and be a "not there" value. Don't treat it as or expect it to be a 0, or an empty string or an empty list. Those are all valid values and can be geniunely valid values in many circumstances - the idea of a null instead means there is no value there.

Perhaps it's a little bit like a function throwing an exception instead of returning a value. Except instead of manufacturing and returning an ordinary value with a special meaning, it returns a special value that already has a special meaning. If a language expects you to work with null, then you can't really ignore it.

🌐
Khoury College of Computer Sciences
khoury.northeastern.edu › home › kenb › MeaningOfNull.html
The Meaning of Null in Databases and Programming Languages
The main distinction between the relational null and the programming language null is the following: The relational null represents the absence of a value in a field of a record; whereas the programming language null represents one of the possible values of a variable.
🌐
Upwork
upwork.com › resources › articles › {name}
Null in Java: Understanding the Basics - Upwork
August 5, 2024 - In Java, null is a literal, a special constant you can point to whenever you wish to point to the absence of a value. It is neither an object nor a type (a common misconception some newcomers to the Java language grapple with).
Find elsewhere
🌐
W3Schools
w3schools.com › mysql › mysql_null_values.asp
MySQL NULL Values - IS NULL and IS NOT NULL
A field with a NULL value is a field with no value. If a field in a table is optional, it is possible to insert a new record or update a record without adding a value to this field.
🌐
ThoughtCo
thoughtco.com › definition-of-null-958118
What Does Null Mean in C, C++ and C#?
April 27, 2019 - In a database, zero is a value. The value null means that no value exists. When used as a value, null is not a memory location. Only pointers hold memory locations. Without a null character, a string would not correctly terminate, which would ...
🌐
Wikipedia
en.wikipedia.org › wiki › Null_object_pattern
Null object pattern - Wikipedia
October 20, 2025 - The null object design pattern, which describes the uses of such objects and their behavior (or lack thereof), was first published as "Void Value" and later in the Pattern Languages of Program Design book series as "Null Object". In most object-oriented languages, such as Java or C#, references may be null. These references need to be checked to ensure they are not null before invoking any methods, because methods typically cannot be invoked on null references. The Objective-C language takes another approach to this problem and does nothing when sending a message to nil; if a return value is expected, nil (for objects), 0 (for numeric values), NO (for BOOL values), or a struct (for struct types) with all its members initialised to null/0/NO/zero-initialised struct is returned.
Top answer
1 of 5
7

Actually, you can use a literal 0 anyplace you would use NULL.

Section 6.3.2.3p3 of the C standard states:

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

And section 7.19p3 states:

The macros are:

NULL

which expands to an implementation-defined null pointer constant

So 0 qualifies as a null pointer constant, as does (void *)0 and NULL. The use of NULL is preferred however as it makes it more evident to the reader that a null pointer is being used and not the integer value 0.

2 of 5
5

NULL is used to make it clear it is a pointer type.

Ideally, the C implementation would define NULL as ((void *) 0) or something equivalent, and programmers would always use NULL when they want a null pointer constant.

If this is done, then, when a programmer has, for example, an int *x and accidentally writes *x = NULL;, then the compiler can recognize that a mistake has been made, because the left side of = has type int, and the right side has type void *, and this is not a proper combination for assignment.

In contrast, if the programmer accidentally writes *x = 0; instead of x = 0;, then the compiler cannot recognize this mistake, because the left side has type int, and the right side has type int, and that is a valid combination.

Thus, when NULL is defined well and is used, mistakes are detected earlier.

In particular answer to your question “Is there a context in which just plain literal 0 would not work exactly the same?”:

  • In correct code, NULL and 0 may be used interchangeably as null pointer constants.
  • 0 will function as an integer (non-pointer) constant, but NULL might not, depending on how the C implementation defines it.
  • For the purpose of detecting errors, NULL and 0 do not work exactly the same; using NULL with a good definition serves to help detect some mistakes that using 0 does not.

The C standard allows 0 to be used for null pointer constants for historic reasons. However, this is not beneficial except for allowing previously written code to compile in compilers using current C standards. New code should avoid using 0 as a null pointer constant.

🌐
Medium
medium.com › @sumeet2703 › understanding-the-difference-between-zero-and-null-in-programming-db60b4e12934
Understanding the Difference Between Zero and Null in Programming | by Sumeet K | Medium
September 4, 2024 - ... x = 0 # x is an integer with a value of zero y = 5 + x # y will be 5, since adding zero does not change the value · Null, on the other hand, represents the absence of a value or a reference.
🌐
Medium
medium.com › @sikirus81 › null-vs-void-whats-the-difference-1f8f6b4801b6
Null vs Void: What’s the Difference? | by sikiru | Medium
January 8, 2024 - When a variable is declared but not initialized, its default value is null. For example: ... Any object reference can be set to null. This removes the association between the variable and the object.
🌐
freeCodeCamp
freecodecamp.org › news › a-quick-and-thorough-guide-to-null-what-it-is-and-how-you-should-use-it-d170cea62840
A quick and thorough guide to ‘null’: what it is, and how you should use it
June 12, 2018 - As we can see, in case of a value type, the value itself is directly stored at the address A0A1 which is associated with variable name. There would be much more to say about reference versus value types, but this is out of the scope of this article. Please note also that some programming languages support only reference types, others support only value types, and some (e.g. C# and Java) support both of them. ... The concept of null exists only for reference types.
🌐
Wikipedia
en.wikipedia.org › wiki › Null_character
Null character - Wikipedia
6 days ago - The null character is a control character with the value zero. Many character sets include a code point for a null character – including Unicode (Universal Coded Character Set), ASCII (ISO/IEC 646), Baudot, ITA2 codes, the C0 control code, ...
🌐
Quora
quora.com › What-does-NULL-mean-in-programming-languages
What does NULL mean in programming languages? - Quora
Answer (1 of 6): null is the pointer or reference to address zero. Why would this be needed or used? Let’s say you are searching for a library book that you want to read. Unlike Amazon, your library doesn’t have every book.
Top answer
1 of 11
130

The problem isn't null itself. It's the implicit nullability of all object references, as is the case in Java, Python, Ruby (and previously C#, although that picture is changing).

Imagine you're using a language where every type T really means T | Null. Suppose you had a system that takes such a nullable reference at its entry point, does some null checking, and wants to forward on to another function to do the "meat" of the work. That other function has no possible way to express "a non-null T" in Java (absent annotations), Python or Ruby. The interface can't encode its expectation for a non-null value, thus the compiler can't stop you from passing null. And if you do, fun things happen.

The "solution" (or "a solution", I should say), is to make your language's references non-nullable by default (equivalently, not having any null references at all, and introducing them at the library level using an Optional/Option/Maybe monad). Nullability is opt-in, and explicit. This is the case in Swift, Rust, Kotlin, TypeScript, and now C#. That way, you clearly distinguish T? vs T.

I would not recommend doing what you did, which is to obscure the nullability behind a type alias.

2 of 11
10

You are probably looking for optionals. Here, for example, in TypeScript that you use:

http://dotnetpattern.com/typescript-optional-parameters

The idea is to have explicitly state:

  • A) if an object can be null or not
  • B) unwrapping nullable objects before using them to avoid null pointer exceptions (NPEs)

The TypeScript parameter example I linked does not really do B)

if (address == undefined) {
  // Do something with a
} else {
  // Address not found, do something else
}

Maybe you can find a better way in TypeScript like it is done in Swift:

if let a = address {
  // Do something with a
} else {
  // Address not found. Do something else.
}

Or Kotlin:

address?.let { a ->
  // Do something with a
} ?: {
  // Address not found. Do something else
}()

The difference is that in the TypeScript example above you can still forget the check and just address, leading to an NPE. Optionals like in Swift or Kotlin will force you to first check the value existence before you can use them.

A common mistake done then is to force unwarp the value because you think the value always exists:

address!.doSomething() // In Swift

address!!.doSomething() // in Kotlin

If you see code like this, run! Usually a value is an optional for a reason, so just skipping over that safety-measure and claiming "there is a value, I am sure" leads you back to the old NPE-coding we try to get rid of.

A quick search looks like you are not really able to do that in TypeScript without voodoo:

Is it possible to unwrap an optional/nullable value in TypeScript?

🌐
Programming Duck
programmingduck.com › home › articles › nulls and null checks – how to work safely with nulls in any codebase
Nulls and null checks - How to work safely with nulls in any codebase - Programming Duck
January 4, 2022 - The problem is that something in your code might be null when it’s not supposed to be. In other words, you have a bug. But, if you have null checks where they’re not needed, you’ll silently ignore the bug. It will be swallowed up in a null check. ... In the code above, car may be null when it’s not supposed to be. That’s a bug. However, due to an unnecessary null check, the program won’t crash.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › null-pointer-in-c
NULL Pointer in C - GeeksforGeeks
The Null Pointer is the pointer that does not point to any location but NULL. According to C11 standard: “An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.
Published   January 10, 2025