0 and null

exercise No. 205

Q:

Consider the following two code snippets:

  • if (0 == someVariable) {/* Code omitted for brevity */}

  • if (null == someOtherVariable) {/* Code omitted for brevity */}

Answer the following two questions:

  1. What is the difference between 0 and null?

  2. Assuming the above snippets are part of well compiling Java code: What do we know about the possible data types of someVariable and someOtherVariable ?

A:

    • 0 is a literal denoting the value zero of type int.

    • A variable of class or array type of value null does not reference an object.

    • someVariable must be of type byte, short, int, long, char, float or double. In other words: Any primitive type except boolean.

    • someOtherVariable must be of either class / enum or array type.