Suppose that we say:

Cons lst = null;
for (int i = 0; i < 3; i++ )
    lst = cons( i, lst );

What is the type of first(lst) as seen by the compiler?

  • A: int
  • B: Integer
  • C: short
  • D: Cons
  • E: Object

    Answer: E

    The int value i gets coerced to Integer by the compiler before doing the cons. A smart compiler might infer that the first(lst) would now be Integer, but the Java compiler is rather stupid and only knows the declared type of first(), which is Object. If we want to use the type of first(lst), we will have to cast it as ((Integer) first(lst)).

    Contents    Page-10    Prev    Next    Page+10    Index