Case Sensitivity
null is case sensitive. It is null, and not Null or nUll or NULL, etc.
Class and Instantiated object
A class is a set of variables and methods that work together. These variables are called fields (properties). When these fields are given values, the resulting unit is called an object. Having an object from a class is instantiating the object. Null can be assigned to an object, but zero cannot be assigned. The following Java program compiles into a byte-code and runs without any error message:
int prop;
int mtthd() {
return prop;
}
}
class TheClass {
public static void main(String[] args) {
Cla obj1 = new Cla();
Cla obj2 = null;
}
}
TheClass is the class for the main() method. The class of interest here is Cla. Note that obj2 has taken null and not zero in the main() method.
in the main() method would have resulted in an error message.
null and Zero Nuance with Primitive Types
Java has primitive data types in normal use and has primitive data class (wrapper) types. For example, the following code is legitimate in Java:
i is the normal declaration of a primitive integer type, while I am the declaration of an object of the Integer class. d is the normal declaration of a primitive double type, while D is the declaration of an object of the Double class.
Here is the nuance: i takes 0, while I takes null for the same purpose; d takes 0, while D takes null for the same purpose. The following Java program compiles into a byte-code and runs without any error message:
The following code would result in two error messages by the compiler:
I and D can take 0 or null as value, as they should. However, i and d cannot take null as value, which is the real nuance, and cause of the two error messages.
The instanceof Operator
The instanceof operator returns true if the object is the instance (instantiated object) of a particular class. The syntax is:
The output of the following program is true:
If null was assigned to obj as follows:
The output would have been false.
Zero is not null in Java
The following program does not compile because zero is of type int, while null is of type, <null> :
Zero cannot be null in Java.
Kinds of Types and Values
There are two kinds of types in Java: the primitive type and the reference type. Primitive types are boolean, int, double, and their variants. These have their values. There are four kinds of reference types: class types, interface types, type variables, and array types. These, too, have their values. Null is a type whose only value is null. The following program illustrates how a primitive int type and value can be declared:
The output is 5. The null type cannot be declared similarly. Null is normally used as a value and not in type declaration. That is,
is not possible.
Note: the name of an instantiated object is a reference.
Primitive Classes and null
Integer, beginning with uppercase I, is the primitive class of the int type. Double, beginning with uppercase D, is the primitive class of the double type. Boolean, beginning with uppercase B, is the primitive class of the boolean type. These class types would take null or zero, while their equivalent primitive types would take zero only. The following program illustrates this:
The program compiles without any issue. Note: neither Boolean nor boolean takes 0 for false.
null, printed as null
Just as true and false are printed in words, null is also printed as a word in Java. The following program illustrates this:
The output is:
true
null
null as Array Value
null can be used as a value in an array if the values are of a class type. The following program illustrates this:
The output is:
Note that null as an element is not in quotes. Here, String is a class. It is imported by default. It does not have to be imported manually.
The whole array is an object. So null can be assigned to an array name. The following program illustrates this:
The output is: null.
Conclusion
When dealing with primitive types, assign zero to the variable when you have to. When dealing with class types, you can assign zero or null depending on the context. Neither Boolean nor boolean takes 0 for false. In Java, zero and null are not interchangeable.