The null Value

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        
String a;
Point  b;

Answer:

  • 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 reference objects, once there are some.

The null Value

A reference variable holds information about the location of an object. It does not hold the object itself. This code

String a;
Point  b;

declares two reference 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 );

null is a special value that means "no object." Your program should set a reference variable to null when it is not referring to any object. Programs often set variables to null when they are declared:

String a = null;
Point  b = null;

QUESTION 4:

(Thought Question:) Do you think that null can be assigned to reference variables of any type? Click here for a .