<< May 29, 2008 | Home | May 31, 2008 >>

Friday Java Quiz: Generics

Today's quiz is inspired by the free Chapter of Joshua Bloch's Effective Java, 2nd Edition available from InfoQ.

There was a short time early last year when I used generics on the project, until I received the memo that said "We are using JDK 5 mainly for its runtime performance improvements. You can't actually use any of the JDK 5 language features." So my generics skills have rusted a little. The free chapter gave me a much needed refresher on generics.

Here's todays question:

Q: Will the following method compile?

public static <T extends Comparable<? super T>> T max(
        List<? extends T> list) {
    Iterator<T> i = list.iterator();
    T result = i.next();
    while (i.hasNext()) {
        T t = i.next();
        if (t.compareTo(result) > 0)
            result = t;
    }
    return result;
}

The answer is in the sample chapter. Go read it.

Tags :