ArticleslgStudy

science

Nullable type

Nullable type is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Nullable type rather than just read about it. In short: Nullable types are a feature of some programming languages which allow a value to be set to the special value NULL instead of the usual possible values of the data type. In statically typed languages, a nullable type is an option type, while in dynamically typed languages (where values have types, but variables do not), equivalent behavior is provided by having a single null value.

Key takeaways

  • Nullable type belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Nullable type to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Nullable type from memory before moving on to harder problems.

Reference excerpt

Nullable types are a feature of some programming languages which allow a value to be set to the special value NULL instead of the usual possible values of the data type. In statically typed languages, a nullable type is an option type, while in dynamically typed languages (where values have types, but variables do not), equivalent behavior is provided by having a single null value. NULL is frequently used to represent a missing value or invalid value, such as from a function that failed to return or a missing field in a database, as in NULL in SQL. In other words, NULL is undefined. Primitive types such as integers and Booleans cannot generally be null, but the corresponding nullable types (nullable integer and nullable Boolean, respectively) can also assume the NULL value. This can be represented in ternary logic as FALSE, NULL, TRUE as in three-valued logic.

Example An integer variable may represent integers, but 0 (zero) is a special case because 0 in many programming languages can mean "false". Also, this does not provide any notion of saying that the variable is empty, a need that arises in many circumstances. This need can be achieved with a nullable type. In programming languages like C# 2.0, a nullable integer, for example, can be declared by a question mark (int? x). In programming languages like C# 1.0, nullable types can be defined by an external library as new types (e.g. NullableInteger, NullableBoolean). A Boolean variable makes the effect more clear. Its values can be either "true" or "false", while a nullable Boolean may also contain a representation for "undecided". However, the interpretation or treatment of a logical operation involving such a variable depends on the language. The following is an example of T? (or explicitly System.Nullable<T>) in C#.

Compared with null pointers In contrast, object pointers can be set to NULL by default in most common languages, meaning that the pointer or reference points to nowhere, that no object is assigned (the variable does not point to any object). Nullable references were invented by C. A. R. Hoare in 1965 as part of the Algol W language. Hoare later described his invention as a "billion-dollar mistake". This is because object pointers that can be NULL require the user to check the pointer before using it and require specific code to handle the case when the object pointer is NULL. Java has classes that correspond to scalar values, such as Integer, Boolean, and Float. Combined with autoboxing (automatic usage-driven conversion between object and value), this effectively allows nullable variables for scalar values.

Compared with option types Nullable type implementations usually adhere to the null object pattern. There is a more general and formal concept that extend the nullable type concept: it comes from option types, which enforce explicit handling of the exceptional case.

Language support The following programming languages support nullable types. Statically typed languages with native null support include:

Ballerina C# Dart Kotlin Ceylon SQL SAS (Missing values) Statically typed languages with library null support include:

C# (since version 2) Delphi Free Pascal VB.NET Java (since version 8) Scala Oxygene F# Statically typed CLI languages Dynamically-typed languages with null include:

JavaScript has a null and undefined values. Julia has the nothing value (which is of type Nothing) and the Union{T, Nothing} type idiom. Perl scalar variables default to undef and can be set to undef. PHP with NULL type and is_null() method, native nullable type in version 7.1 Python has the None value. Ruby has the nil value and NilClass type.

See also Null coalescing operator Semipredicate problem Union type Unit type

References

Worked examples

Example 1 — a first encounter with Nullable type

Start with the simplest possible case. Write down what Nullable type claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Nullable type before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Nullable type ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Nullable type

In research
Nullable type appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Nullable type in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Nullable type is common in secondary-school and first-year university syllabi. It links to neighbouring topics Type theory, so understanding it makes those chapters shorter.
In everyday life
Look for Nullable type outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Nullable type in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Nullable type means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Nullable type out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Nullable type in simple terms?

Nullable types are a feature of some programming languages which allow a value to be set to the special value NULL instead of the usual possible values of the data type. In statically typed languages, a nullable type is an option type, while in dynamically typed languages (where values have types…

Why does Nullable type matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Nullable type?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Nullable type.

Tags

  • Type theory

Keep exploring