Java

Null in Java

Null means nothing, and void means empty. Void is a type, while null is more of a value than a type in its use. In Java, null is not interchanged with zero. This article explains how null is used in Java.

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:

    class Cla {
        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.

Cla obj2 = 0;

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:

            int i = 5;
            Integer I = 5;

            double d = 6.6;
            Double D = 6.6;

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:

    class TheClass {

        public static void main(String[] args) {  
            int i = 0;
            Integer I = null;

            double d = 0.0;
            Double D = null;
        }  
    }

The following code would result in two error messages by the compiler:

            int i = null;
            Integer I = 0;

            double d = null;
            Double D = 0.0;

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:

object instanceof class

The output of the following program is true:

    class Cla {
        int prop;
        int mtthd() {
            return prop;
        }
    }

    class TheClass {

        public static void main(String[] args) {  
            Cla obj = new Cla();
            boolean bl = obj instanceof Cla;
            System.out.println(bl);
        }  
    }

If null was assigned to obj as follows:

Cla obj = null;

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

    class TheClass {

        public static void main(String[] args) {  
            if (0 == null)
                System.out.println("Zero can be 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:

    class TheClass {

        public static void main(String[] args) {  
            int i = 5;
            System.out.println(i);
        }  
    }

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,

null i = null;

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:

    class TheClass {

        public static void main(String[] args) {  
            Integer a = 0;
            Integer b = null;
            int c = 0;

            Double d = 0.0;
            Double e = null;
            double f = 0;

            Boolean g = false;
            Boolean h = null;
            boolean i = false;
        }  
    }

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:

    class TheClass {

        public static void main(String[] args) {  
            Boolean a = false;
            Boolean b = true;
            Boolean c = null;
   
            System.out.println(a);
            System.out.println(b);
            System.out.println(c);
        }  
    }

The output is:

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

    class TheClass {

        public static void main(String[] args) {  
            String[] str = {"one", null, "two", null, "three"};
            System.out.println(str[0]+' '+str[1]+' '+str[2]+' '+str[3]+' '+str[4]);
        }  
    }

The output is:

one null two null three

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:

    class TheClass {
        public static void main(String[] args) {  
            String[] str = null;
            System.out.println(str);
        }  
    }

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.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.