2

In java when we assign null to any object like this:

private Apple apple = null;

Is there any class in java related to this null?

Because in Apache common-lang jar they have defined null like this:

public static class Null implements Serializable {
            /**
             * Required for serialization support. Declare serialization compatibility with Commons Lang 1.0            *             * @see java.io.Serializable
             */
           private static final long serialVersionUID = 7092611880189329093L;

            /**
            * Restricted constructor - singleton.
             */
            Null() {
               super();
            }

           /**
             * <p>Ensure singleton.</p>
            * 
           * @return the singleton value
             */
            private Object readResolve() {
                return ObjectUtils.NULL;
            }
       }

Why they have made it like this?

or it is just a bit pattern?

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
  • 1
    Note that `null instanceof AnyType` is always `false`, whatever `AnyType` is. (`null instanceof Null`, where `Null` is Apache's class, is also `false`). – Jesper Jul 17 '15 at 07:14

2 Answers2

7

In java null is not an object and therefore does not have a class.

It does have a type though - see the language specification.

Apache has created a class called Null (not null). This is an entirely different thing. I'm guessing they created it so they could represent null with an actual object - which may make programming easier in some ways.

dave
  • 11,641
  • 5
  • 47
  • 65
0

In java null is a keyword, not a class or object.

Anjali
  • 1,623
  • 5
  • 30
  • 50