Null Value & is Operator

You will learn about the use of null value and the is operator in this lesson.

We know that a variable of a reference type needs not reference a particular object:

MyClass referencesAnObject = new MyClass;
MyClass variable; // does not reference an object

Being a reference type, the variable above does have an identity but it does not reference any object yet. Such an object can be imagined having a place in memory, as in the following picture:

A variable that does not reference any value is null. We will expand on this below.

Such a variable is in an almost useless state. Since there is no MyClass object that it references, it cannot be used in a context where an actual MyClass object is needed:

Press + to interact
import std.stdio;
class MyClass {
int member;
}
void use(MyClass variable) {
writeln(variable.member); // ← BUG
}
void main() {
MyClass variable;
use(variable);
}

As there is no object that is referenced by the parameter that use() receives, attempting to access a member of a non-existing object results in a program crash.

Segmentation fault is an indication that the program has been terminated by the operating system because of an attempt to access an illegal memory address.

svg viewer

The null value

The special value null can be printed just like any other value.

Press + to interact
import std.stdio;
void main () {
writeln(null);
}

A null variable can be used only in two contexts:

  • Assigning an object to it:

    variable = new MyClass; // now references an object
    

    The assignment above makes the variable provide access to the newly constructed object. From that point on, the variable can be used for any valid operation of the MyClass type.

  • Determining whether it is null

    However, because the == operator needs actual objects to compare, the expression below cannot be compiled:

    if (variable == null) // ← compilation ERROR
    
Press + to interact
class MyClass {
int member;
}
void main() {
MyClass variable;
variable = new MyClass;
if(variable == null) {} //compilation error
}

For that reason, whether a variable is null must be determined by the is operator.

The is operator

This operator answers the question “does have the null value?”:

if (variable is null) {
    // Does not reference any object
}

is can be used with other types of variables as well. In the following use case, it compares the values of two integers:

Press + to interact
import std.stdio;
void main() {
int speed = 120;
int newSpeed = 120;
if (speed is newSpeed) {
writeln("Their values are equal");
} else {
writeln("Their values are different");
}
}

When used with slices, it determines whether the two slices reference the same set of elements:

if (slice is slice2) {
    // They provide access to the same elements
}

The !is operator

!is is the opposite of is. It produces true when the values are different:

if (speed !is newSpeed) {
    // Their values are different
}