String a;
Point  b;

A good answer might be:

  • What is the data type of the variable a?
    • a is a reference to a String object.
  • What is the data type of the variable b?
    • b is a reference to a Point object.
  • How many objects are there (so far)?
    • So far, there are no objects, just variables that can be used to keep track of objects once there are some.

The null Value

A reference variable can hold information about the location of an object, but does not hold the object itself. This code...

String a;
Point  b;

... declares two variables but does not construct any objects. The following constructs objects and puts references in the variables:

a = "Elaine the fair." ;
b = new Point( 23, 491 );

There is a special value called null that can be assigned to an object reference variable.

The value null is a special value that means "no object." A reference variable is set to null when it is not referring to any object.

In most programs, objects come and objects go, depending on the data and on what is being computed. (Think of a computer game, where monsters show up, and monsters get killed). A reference variable sometimes does and sometimes does not refer to an object. You need a way to say that a variable does not now refer to an object. You do this by assigning null to the variable. Inspect the following:

class nullDemo1
{
  public static void main (String[] arg)
  {
    String a = "Random Jottings";
    String b = null;
    String c = "";

    if ( a != null ) 
      System.out.println(  a );

    if ( b != null ) 
      System.out.println(  b );

    if ( c != null ) 
      System.out.println(  c );
  }
}    

Variables a and c are initialized to object references. Variable b is initialized to null.

QUESTION 3:

What exactly is variable c initialized to? Click here for a

Click Here after you have answered the question