Programming Languages (CS302 2006S)

Notes on Java Generics

Comments on:

Bracha, G. (2004). Generics in the Java Programming Language.

Comments from: Peter Brown, Michael Claveria (-), Alex Leach.

No Comments from: Davis Hart (x), Brad Miller (x), Angeline Namai (x), Mark Nettling (x), Dimitar Tasev (x), Dave Ventresca (x).


On page 18 of the Java Generics Tutorial (Section 9), it says that the call to writeAll() is illegal, because Collection is cast as a String, and Sink is cast as an Object. If this is the case, what is the purpose of the <T> casts in the parameters of writeAll(Collection <T> coll, Sink <T> snk)?

I wouldn't say that they are cast as String and Object, I would say that they are specialized to (or parameterized by) those types. In the first version of writeAll the compiler must assume that the programmer's intent is to ensure that the two type parameters are identical. Since they are not, the code is unacceptable.

I thought the tutorial was pretty straight forward overall. I couldn't really find that much specific stuff to comment over so I thought I would just give a brief description of my understanding of generics instead. I see generics as very helpful when we deal with collections of objects, especially when we only want collections of a given type. Instead of specifying a new collection class for every concievable type, generics introduce an easy form of polymorphism.

I'm kind of tired so most of my comments are more or less notes from the reading.

aList myIntList = new LinkedList();
myIntList.add(new Integer(0));
Integer x = (Integer) myIntList.iterator().next();

included a cast in line 3 to make sure that the assignment of x to a variable of type Integer is type safe since myIntList.iterator() can only ensure an Object type. But the later code

List<Integer> myIntList = new LinkedList();
myIntList.add(new Integer(0));
Integer x = myIntList.iterator().next();
is an example where myIntList is now specified as a list of integers and thus doesn't need the casting in the third line

Wildcards are really cool. Collection<?> Cannot add to it because we don't know type but we can call get and make use of the result.

public void drawAll(List<? extends Shape> shapes) {...} the addition of ? extends now allows draw all to accept lists of any subclass of Shape. Solves the problem that Bjorne S. talked about with the firetruck, police car and other vehicles making a right turn. However this means we cannot write shapes in the body of the method.

We can use a generic method to shove objects in an array into a collection. Wildcards support flexible subtyping, generic methods allow type parameters to be used to express dependencies among types of arguments to a method/ the methods return type.

Wildcards with a lower bound can be used to write a collection or have a set of comparators How does wildcard capture work?

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Wed May 10 09:03:14 2006.
The source to the document was last modified on Fri Mar 17 08:39:44 2006.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS302/2006S/Readings/generics.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu