public class NullDemo   
{
  public static void main (String[] arg)   
  {
    String a = "abracadabra"; // 1. an object is created; 
                              //    variable a refers to it
    String b = null;          // 2. variable b refers to no
                              //    object.
    String c = "";            // 3. an object is created 
                              //    (containing no characters)
                              //     variable c refers to it
    if (a != null)            // 4. is true, so the
       System.out.println(a); // 5. the println(a) executes.
                                              
    if (b != null)            // 6. is false, so the
       System.out.println(b); // 7. println(b) is skipped.

    if (c != null)            // 8. is true, so the
       System.out.println(c); // 9. println(c) executes (but
                              //    it has no characters to 
                              //    print).
  }
}    

Back to Lesson 10 Examples

Back to Java Main Page