Exercise 5.x4:
With a copying garbage collector, why is that most objects that are allocated by a typical program are not copied at all?
With a nongenerational copying garbage collector, why is it that most objects that are copied at all are copied repeatedly?
Briefly explain how a generational copying collector is typically able to collect lots of garbage without doing much copying, and in particular without much repeated copying of the same objects.
Exercise 6.x1: For each of the following two Java statements, indicate whether it is legal, and if so, explain under what circumstances each exception handler would execute. The first version is
try { System.in.read(); } catch(java.io.CharConversionException e2){ System.err.println("Caught a CharConversionException."); } catch(java.io.IOException e1){ System.err.println("Caught an IOException."); }
and the second version is
try { System.in.read(); } catch(java.io.IOException e1){ System.err.println("Caught an IOException."); } catch(java.io.CharConversionException e2){ System.err.println("Caught a CharConversionException."); }
Exercise X.1: For each of the following three programs, answer the following questions:
Does this program illustrate a covariant return type or a contravariant return type?
Is this program legal Java?
Does the subclass satisfy Liskov and Wing's definition of a subtype? Briefly explain.
Program 1:
abstract class Producer { abstract Object produce(); // returns a new Object each time } class IntegerProducer extends Producer { Integer produce(){ return new Integer(42); } }
Program 2:
abstract class IntegerProducer { abstract Integer produce(); // returns a new Integer each time } class Producer extends IntegerProducer { Object produce(){ return new Object(); } }
Program 3:
abstract class Producer { abstract Object produce(); // returns a new Object each time } class IntegerProducer extends Producer { Integer answer = new Integer(42); Integer produce(){ return answer; } }