Week 4 Answers


  1. Get the "Person" class to compile. Note that you'll need to make some edits - the code above is not entirely correct.

    You probably will need to add an import java.awt.* statement at the top, or to specify java.awt.Color when declaring the "color" member.

  2. Add a field to "Person" which will contain the date and time at which each "Person" object was created.

    You'll need to add a field to the class definition something like this:

         class Person {
               int age;
               String name;
               Color hairColor;
    	   final java.util.Date creationDate;
               . . .
    

    and then make sure that every time an object is constructucted, you have:

               creationDate = new java.util.Date();
    

    Note that final can be used in the "creationDate" field definition for a field that will only be assigned to once from within a constructor - ensures that once it's initialized, it'll never change. The compiler will force you to initialize such fields in a constructor.

  3. What happens if you get your program to do:
        Person me = new Person("nick",21);             // 1
        System.out.println("the person is " + me);     // 2
    

    You should see something like:

       nick is Person@1bab50a
    
    Change your class so that line "2" prints out:
        the person is nick, who is 21 and was created on xxxxx
    
    where "xxxxx" is the date/time that the object was created

    You need to add a "toString" method to your class, something like this:

           public String toString() 
           {
              return name + ", who is " + age + " and was created on " +
    	         creationDate;
           }
    
  4. Add a loop to your program:
             for (int i=0; i<1000; i++) {
                 Person pp = new Person("test",0);
             }
    
    How often do you think the garbage collector will run for this program? Run the program with garbage collection logging turned on and find out

    Use java -verbose:gc to see this. But you can't predict in advance how often it will run. Specifically, it probably won't run as the program exits.

  5. Add the line System.gc() to the end of the program. Run it again with GC logging, what happens?

    Most likely this will cause an extra garbage collection. But there's no guarantee of this.

  6. Add several more consecutive System.gc() lines, run with logging and see what happens.

    Most likely each will cause the garbage collector to run, although consecutive runs of the garbage collector won't find any extra free memory

  7. Find another class in the Java class libraries which has factory methods. Explain why they're used in this case.
  8. java.net.InetAddress has no public constructors, only factory methods. It caches addresses which have been successfully translated to save having to do duplicate DNS lookups.

  9. Explain the difference between the following two lines of code:
         String myString = new String("hello"); // 1
    
         String myString = "hello"; // 2
    

    "hello" is an instance of String. Line 2 says you want to make myString be a reference to that object. Line 1 says that you want to create a brand new String object (using the String constructor), using "hello" as the constructor parameter, and then make myString refer to that newly created object. So both lines will have the same effect (myString will end up referring to "hello"), but line 1 creates an extra reference and incurs the overhead (which in a loop might become significant) of running the String constructor code.

    Examples which show this up more clearly would be

         String a = "hello";
         String b = new String(a);  // 1: c-tor and extra reference unneccessary!
         String b = a;              // 2: the efficient way to do it
    
    or, perhaps a more likely mistake:
         java.util.Date d1,d2,d3;
         d1 = new Date();             // d1 has today's date
         d2 = new Date(d1.getTime()); // 1: another object created, but no need!
         d3 = d1;                     // 2: the efficient way to do it
    

    Joshua Bloch "Effective Java" item 4 discusses this.


Back to index page